Ads*_*ads 6 c++ oop
假设我有两个类A,而B. B.从A派生.A没有数据成员,但B有两个整数成员.
如果我在A类中定义一个方法,如下所示:
void CopyFrom( const A* other ) { *this = *other; }
并在子类中调用它,整数数据成员是否会被复制?
Oli*_*rth 7
不.这被称为切片问题.
这是真实的,即使你重载operator=两个A和B:*this = *other只有永远不会解决到A::operator=(const A&)或者B::operator=(const A&)被调用.
operator=
A
B
*this = *other
A::operator=(const A&)
B::operator=(const A&)
归档时间:
14 年,6 月 前
查看次数:
231 次
最近记录: