How to initialize an array of class instances

Hi to everyone,
I think my question might be elementary for most of you. I've recently begun to learn C++. Now I need to make an array of a class which needs to receive an initializer variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class cStudent
{
public:
	bool shouldBeGraduated;
	int studentAge;
	//cStudent(int age){}
	cStudent(int age)
	{
		studentAge = age;
		if (age > 18)
			shouldBeGraduated = true;
		else
			shouldBeGraduated = false;
	}
};

If I want to create a new instance, I should use this:
1
2
	cStudent *st1;
	st1 = new cStudent(15);

But I need to create an array of 500 instances of this class. I checked these, but I received just compile error
1
2
	st1 = new cStudent[500](15);
	st1 = new cStudent(15)[500];

Also I checked this one:
 
	st1 = new cStudent[500]{ 12 }; // or { {12} }; 

In this case only the first element would receive the variable if the default constructor is uncommented. I don't know how to initialize an array of class instances just by one initializer.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <memory>

class cStudent
{
public:
    bool shouldBeGraduated;
	int studentAge;

	cStudent( int age = last_objects_age )
	{
		last_objects_age = studentAge = age;
		shouldBeGraduated = studentAge > 18 ;
	}

	static int last_objects_age  ;
};

int cStudent::last_objects_age = 0 ;

int main()
{
    cStudent array[8] { 25 } ;
    for( const cStudent& s : array ) std::cout << s.studentAge << ' ';
    std::cout << '\n' ;

    const int n = 10 ;
    // http://en.cppreference.com/w/cpp/memory/unique_ptr
    std::unique_ptr< cStudent[] > array2( new cStudent[n] { 17 } ) ;
    for( int i = 0 ; i <n ; ++i ) std::cout << array2[i].studentAge << ' ';
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/8cd1a5d6420af663
Someone already answered your initial question. I noticed something else and it doesn't have anything to do with your question, and you might already know it, but in case you don't it could be helpful.

There is a different way to construct your class. It's slightly faster but in a class this simple it really makes no difference. It might be helpful to know in the future though.

Currently calling cStudent c(10); will do a few things. First the compiler will allocate space for 1 bool and one int but not assign any value, the same as calling
1
2
bool shouldBeGraduated;
int studentAge;


Then, the compiler gets to the body of the constructor and starts assigning values to your two variables. You can get around this in C++ with constructor initialization list.

By changing cStudent's constructor to
 
cStudent(int age) : studentAge(age), shouldBeGraduated((age>18)? true : false) {}


You can initialize the two variables with a value instead of creating them and then assigning a value.


If you don't recognize (age>18)? true : false
it's the same as
1
2
3
4
if(age>18)
    return true;
else
    return false;
Last edited on
Thank both of you for your responses. I checked the first solution and it worked properly. In the second response certainly will improve my codes in future large projects.
Topic archived. No new replies allowed.