Take two classes that both can only be created by new. One class is the base and the other is a derivative. The derived class only adds methods.
class Base
{};
class Derived : public Base
{};
Base * b = new Base{}
Derived * d = covert( b );
// - or -
Base * b = new Base{};
convert( b ); // converts Base to Derived
Derived * d = dynamic_cast<Derived *>(b);
What I want to do is take the Base class data that has been allocated and extend/wrap with the derivative via some method or function, convert.
Update:
Building for embedded systems memory is scarce, so I am doing everything I can to reduce that amount of memory allocation. I was just wondering if there was a way to just sort of extend the base class already allocated memory and wrap it with the derivative.
More Updates:
Although the embedded system is ARM and I am currently using LLVM compiler this might not be true in the future. So a standard compliant way is preferred.
If I understand correctly your question one possible solution is to use aggregation instead of inheritance.
Instead of reference we can use a smart pointer if necessary.
This way you avoid the cast by constructing a new Derived object which uses the data from the a Base object.