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.)
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?
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
In C# this wouldn't work because a is never created.:
class ClassMain { ClassA a; public void Run() { a.Test(); } }
Open in new window
go in the ClassMain.h file?I thought of putting it there, but then wondered if that meant every one who did #include ClassMain.h would be instantiating an instance of ClassA?