I am relatively new to the OO side of C++, so please forgive this question, which I imagine is relatively straight-forward;
I have some code, loosely like this
SuperClass* super;
if (useSub1())
{
SubClass1 sub1 (s);
super = &sub1;
}
else if (useSub2())
{
SubClass2 sub2 (s);
super = &sub2;
}
super.someMethod();
It should be noted, that the point here is that the Construction of 'sub' is dependent upon the result of the call to someFunction(). Furthermore, there will be multiple if (...) { } constructs like above, each creating a different Object based on a different SubClass of SuperClass, based on some condition.
In summary then SuperClass has a number of Sub-classes, and I want to create the appropriate object type based on an if condition, then use the 'super' object to manipulate them.
Here is my question, I think by doing what I have sub1 (or sub2 for that matter) will go out of scope, so does this leave me with a pointer to nothing?
I hope it is reasonably clear what I am trying to do, I am just not sure how to code it correctly. Any help or advice is gratefully received.
Thanks in anticipation