#include <iostream>
class A{
public:
A(constchar *name) {
std::cout<<name;
}
};
class B{
public:
A objA;
B();
};
B::B():objA("objectA")
{
/// here
}
int main() {
B objB;
}
The above code is correct for initialize class in another class (I initialize objA("objectA") when defination constructor B)
But I want to initialize objA in constructor B ( ///here )
How to do that? (objA not a pointer )
Challenge : if I declare array : A objA[5];
How to initialize with names is : "objectA1","objectA2","objectA3","objectA4","objectA5",
Thank all.
The above code is correct for initialize class in another class (I initialize objA("objectA") when defination constructor B)
But I want to initialize objA in constructor B ( ///here )
You can't initialize any member ///here, because all members are initialized before ///here starts.
You can only modify (already initialized) members ///here.
I know that : I need " delete objA[i] ;" in B::~B() .Is that enough ? . I hear that, If use pointer , you must Pointers Management and Memory Management . But I just know delete object in destructor .
If you can avoid pointers and dynamic allocations the code often becomes simpler, with less chance of making mistakes. Personally, I wouldn't use pointers and dynamic allocations unless there is a reason for it.