gpt4 book ai didi

C++:在基类的成员函数中复制对象数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:05 27 4
gpt4 key购买 nike

假设我有两个类 A 和 B。B 派生自 A。A 没有数据成员,但是 B 有两个整数成员。

如果我在类 A 中定义一个方法,如下所示:

void CopyFrom( const A* other )
{
*this = *other;
}

然后在子类中调用它,整数数据成员会被复制吗?

最佳答案

没有。这被称为 slicing problem .

即使在 AB 中重载 operator= 也是如此:*this = *other 只会解析为 A::operator=(const A&)B::operator=(const A&) 被调用。

关于C++:在基类的成员函数中复制对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5967944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com