Change a parent class to a derived one

For example
fstream fsA;
fsA.open(file,ios::out);
Then fsA is the same as ofstream, which is derived from fstream.

My questions is:
If I have classMother and classDaughter

After I declare
classMother M1;
How can I change M1 to the type of classDaughter.

Thanks.
You can't.

Think about the class data. How is it stored for a base and derived class. What is the size of a class and what happens to that data when the class is created/destroyed.
The only proper way you could do it would be trough pointers and casting.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
class A
{
};
class B: public A
{
};

int main()
{
    A* tobecast = new B();
    B* casted = (B*) tobecast;
};
Thanks. I got my code working by pointer and virtual functions.
Topic archived. No new replies allowed.