Is this even possible in C++?
I want to create a classA object that contains a classB object.
If a classB object is passed in to the classA constructor, it becomes part of classA.
If a classB object is NOT passed to the constructor, classA constructor instantiates a default classB object.
Here is the tricky bit: classB is polymorphic, with subtypes classB1 and classB2.
In the preceding example, ObjB1 is instantiated in global space, so it will consume RAM if it's used or not.
In the real program, ObjB1 will be large.
This is a concern because the code will be compiled for an Arduino micro controller with little RAM.
Is there a way to instantiate the default ObjB1 in the classA() constructor?
Sorry I didn't mention that that classA is for a library.
Most users of the library will only use the default ObjB1.
The purpose of the default classA() constructor is so that users don't have to know about ObjB1.
Only users that need advanced functionality will instantiate a classB object and pass it to the classA(classB& refObjB) constructor.
So that's why I was looking for a way to instantiate a classB1 object in the default classA() constructor.