What is the preferred method for initializing a derived class that was casted from it's base class?
Consider the following scenario:
class A{
public:
A();
~A();
}
class B : public A{
public:
B() {m_b = 0.0;};
~B();
float GetValue(){return m_b;};
private:
float m_b;
}
A* a = new A;
B* b = static_cast<B*>(a);
float val = b->GetValue(); // This was never initialized because it was not constructed
My current solution is to manually call an Initialize() function which would perform the necessary initializations as the constructor would.
It seems sloppy though and there must be a better/cleaner method.
Any help and guidance is greatly appreciated!