I have a class (ClassA) with a member array of instances of another class (Classb).
In the constructor, I want to send in a pointer to an existing array, as well as the size of the array.
Is there a way to initialize the member array in the initializes list along with all the other member variables?
If not, how would I initialize it in the constructor body?
Right now ClassB has no default constructor. Does it have to?
Code:
class ClassA{
private:
ClassB myarray[];
}
//this constructor doesn't work
Classa::ClassA(ClassB*, int n):
myarray(B)// "cannot specify explicit initializer for arrays"
{
}
//this constructor doesn't work either
ClassA::ClassA(ClassB*, int n):
myarray(new ClassB[n])//"no appropriate default constructor available"
{
}
I have found one way which compiles:
Code:
class ClassA{
private:
ClassB* myarray;
}
Classa::ClassA(ClassB*, int n):
myarray(B)
{
}
but will this work the way I want it to?
what's the best way to do this?
Thanks!