My question is about creating the instance a of ClassA in the constructor of ClassMain. It looks like a will go out of scope the moment the constructor finishes executing, leaving my pointer aPtr pointing to an object that is all ready for garbage collection.

Is that a problem?

And while I'm at it I might as well ask how this would be done using an initialization list. Or is there an even better way of doing this?

(This will be an unmanaged code project. I like defining ClassA a at compile time rather than using a new statement.)

You have two options. One you have mentioned yourself: use an operator new() to create a new class. You will need to delete the pointer in the ClassMain destructor. This method is useful when there is no default constructor for the ClassA.
Imagine this situation:
class ClassA:
{
public:
ClassA(int a) {_a = a;}
private:
int _a;
};
For the class ClassA there is no default constructor. The only way to create the ClassA variable is to have an int parameter passed in.
In this case you instantiate the variable in your ClassMain constructor like this:

You mentioned that ClassMain needs "a pointer". Any particular reason for this? If not, then what chaau has shown above is just fine... you're simply creating an instance of ClassA within ClassMain.

But by saying you "need a pointer" implies the ClassA object is coming from outside of ClassMain. In that case, what you could do is have a pointer to a ClassA object defined in ClassMain, and initialize it via ClassMain constructors and member functions.

OF course you have to be careful regarding the life cycle of ClassMain and ClassA. If you create a ClassA object and a ClassMain object, and then pass a pointer to the ClassA object to ClassMain, and ClassA becomes invalid (such as goes out of scope), then the pointer in ClassMain becomes invalid. This is where creating the ClassA object within ClassMain can be safer, because that way as the programmer of ClassMain, you control the life cycle of ClassA. Otherwise, passing a pointer into ClassMain means that someone else could use your ClassMain inappropriately.

0

deleydAuthor Commented: 2013-11-27

If I instantiate ClassA in my ClassMain.h file, is there a problem with everyone who does #include "ClassMain.h" at the top of their file ends up instantiating another instance of ClassA, a side effect they don't necessarily want?

both statements would instantiate a object of class ClassMain and with that also a object of ClassA which could be accessed by m.a in the first case and by pm->a in the pointer case.

Note, if you would make the member a private member (like HooKooDooKu showed) only member functions of ClassMain could be access the member a.

Sara

0

deleydAuthor Commented: 2013-11-27

I'm starting to get it. Just including ClassMain.h doesn't instantiate an an instance of that class, it just gives information about the class in case anyone's interested. Creating the instance of ClassMain gives me variable a which is an instance of ClassA, and I don't have to do anything in the constructor of ClassMain to create an instance of ClassA