I have a function something like:
MyClass1 mc1, mc0; //Single Object
MyClass2 mc2; //vector of MyClass1
mc1.Init(1);
mc2.Add(mc1);
mc1 = mc0;
mc1.Init(2);
mc2.Add(mc1);
mc1 = mc0;
//so on......
I actually want to set mc1 = null before at the beginning of the step but cannot do that in C++. So I kept a never-initialized mc0 to do that.
Don’t think this is an elegant solution.
My background was mainly about C# and ASP.NET which is managed.(I think the unmanaged attribute of C++ is the reason why I cannot do object = null. Right?)
This has nothing to do with whether the language is managed or not, but rather with C++’ different approach to object references. In C#, the declaration
MyClass mc;would produce a variable that may refer to aMyClassor benull. In C++,MyClass mc;produces a variable that is aMyClass– the object instance is wholly contained in the variable, and as such, the variable cannot benull. If you want a reference, you would typically use a pointer:MyClass * mc = NULL;orMyClass * mc = new MyClass();.