However, it seems that pAB is always pointing to class A(base) no matter what argument is sent to the constructor. How can I get this type of polymorphism, where I am only allocating memory for the correct class at run time?

Notice the syntax used when declaring "Cobject" and how it changes depending on what you're looking for.

There is one caveat though. Because B is derived from A, it contains all of the public and protected members of the A class. If you use a template in this way, you will not have access to the members of the A class because your pointer is pointing directly at the B object instead of at the embedded A object.

To access the A interface from a B object, you would have to add "wrapper" members to the B class that resolve the scope of the A class for you, which is a lot of extra work.

It would be possible to add a proper polymorphic element to this by creating another case, perhaps called 'P':