打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在编写派生类的赋值函数时,注意不要忘记对基类的数据成员重新赋值
u 在编写派生类的赋值函数时,注意不要忘记对基类的数据成员重新赋值。例如:
class Base
{
public:
Base & operate =(const Base &other); // 类Base 的赋值函数
private:
int m_i, m_j, m_k;
};
class Derived : public Base
{
public:
Derived & operate =(const Derived &other); // 类Derived 的赋值函数
private:
int m_x, m_y, m_z;
};
Derived & Derived::operate =(const Derived &other)
{
//(1)检查自赋值
if(this == &other)
return *this;
//(2)对基类的数据成员重新赋值
Base::operate =(other); // 因为不能直接操作私有数据成员
//(3)对派生类的数据成员赋值
m_x = other.m_x;
m_y = other.m_y;
m_z = other.m_z;
//(4)返回本对象的引用
return *this;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
子类的拷贝构造函数和赋值操作符
C++类成员的初始化
【C/C 】继承
C++对象模型
面向对象C++第三次作业
抽象类 虚析构函数 纯虚析构函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服