به این عمل DownCasting گفته میشه.
برای اینکار می بایستی operator= را در کلاس فرزند برای نوع والد تعریف کنید.به صورت زیر:
class Parent {
public:
Parent() :
value1_(100) {
}
int value1_;
};
class Child : public Parent {
public:
Child() :
Parent(),
value2_(200) {
value1_ = 50;
}
Child& operator=(Parent rhs) {
if (&rhs == this)
return *this;
value1_ = rhs.value1_;
return *this;
}
int value2_;
};
int main()
{
Parent* b = new Parent();
Child* c = new Child();
*c = *b;
return 0;
}