Say I have a class ClassMain
, and ClassMain
needs to have a pointer to a ClassA
. If I do the following:
aPtr = &a;
My question is about creating the instance a
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.)