If I have a class:
class A
{
private:
char z;
int x;
public:
A(char inputz, int inputx);
~A() {}
}
I want to make an array of A
in class B
.
class B
{
private:
A arrayofa[26];
public:
B();
~B() {}
void updatearray(); // This will fill the array with what is needed.
}
class B
{
B:B()
{
updatearray();
std::sort( &arrayofa[0], &arrayofa[26], A::descend );
}
}
How do I explicitly initialize arrayofa
in the constructor of B
?
You can initialize the array variable which is declared inside the class just like any other value, either using constructor or, using the setter method.
One way to initialize the array of objects is by using the constructors. When you create actual objects, you can assign initial values to each of the objects by passing values to the constructor. You can also have a separate member method in a class that will assign data to the objects.
A copy constructor is a member function that initializes an object using another object of the same class.
Each class you declare can optionally provide a constructor with parameters that can be used to initialize an object of a class when the object is created. Java requires a constructor call for every object that's created, so this is the ideal point to initialize an object's instance variables.
The default constructor will be called automatically (for non-POD). If you need a different constructor you're out of luck, but you can use vector
instead which will support what you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With