c++的polymorphism 是透過pointer來達成的
i.e.
class parent {
};
class child {
};
child *c = new child;
parent *p = c;
可是把 child*c = new child ; 換成
child c;
polymophism就沒有了
這時可以用static_cast
parent &p = static_cast< parent& >(c);
記得要用 & 變ref
不然只是多一個無關緊要的 p出來而已
印出他的address就知道了
Wednesday, October 19, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment