I am type casting a class to use storage set by another class. I want to invoke the constructor of the first class when type casting is performed. Is there any way to achieve this?
Consider the following code:
class A{
int x;
public:
A() {x = 0;}
};
class B{
double x;
public:
B() {x =0.0f;}
};
B * bObj = new();
A * aObj = reinterpret_cast < A * > ( bObj ); //Just for the sake of explaining
Now aObj references to the memory allocated to bObj & uses it & stores its contents there. I am trying to get the constructor of A class called when the typecasting is performed or after. Please help.
I want to use aObj and the constructor for A to be called. which doesn't happen. aObj is, no doubt ,getting memory allocated however, the constructor call doesn't happen. And it has garbage values filled in it.