"=" 重载,如何深度copy

joly_zl 2009-02-19 10:13:09
#include <iostream>
using namespace std;

class vehicle
{
public:
vehicle()
{
mMaxSpeed=0;mMaxWeight=0;num=0;
cout<<"vehicle constructor"<<endl;
}
vehicle(int sp,int we,int n)
{
mMaxSpeed=sp;mMaxWeight=we;num=n;

}
~vehicle(){cout<<"vehicle unconstructor"<<endl;}

vehicle(const vehicle& vv)
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;

cout<<"vehicle copy-constructor"<<endl;
}

vehicle& operator =(const vehicle& vv)//"="重载
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
return *this;
}

virtual int getMaxSpeed(){cout<<"get Maxspeed vehicle\n";return mMaxSpeed;}
virtual int getMaxWeight(){cout<<"get MaxWeight vehicle\n";return mMaxWeight;}
virtual void setNum(int n){cout<<"set num vehicle\n";num=n;}

virtual void setMaxSpeed(int speed){cout<<"set Maxspeed vehicle\n"; mMaxSpeed=speed;}
virtual void setMaxWeight(int weight){cout<<"set MaxWeight vehicle\n"; mMaxWeight=weight;}

protected:
int mMaxSpeed;
int mMaxWeight;
private:
int num;
};

class car :public vehicle
{
public:
car()
{
type=0;
cout<<"car constructor\n";
}
car(const car& cc):vehicle(cc)
{
/*mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;*/
type=cc.type;
cout<<"car copy constructor!"<<endl;

}
car& operator =(const car& cc)//如何实现vehicle类中 的private 变量num赋值?????
{
//
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;
type=cc.type;
return *this;
}
private:
int type;
};

int main()
{
//test vehicle the base class;
vehicle v;
vehicle v1(v);
vehicle v2;
v2.setMaxSpeed(33);
v2.setMaxWeight(55);
vehicle v3=v2;
vehicle v4(v2);
int speed=v2.getMaxSpeed();
int weight=v2.getMaxWeight();
v=v2;
//////////////////////test over!
car c;
car c1;
c.setMaxSpeed(12);
c.setMaxWeight(33);
c.setNum(11);

c1=c;
car c2(c);
car c3=c;
car c4;
c4=c;

}
...全文
372 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
waizqfor 2009-02-19
  • 打赏
  • 举报
回复
来过来 学习学习!~
arong1234 2009-02-19
  • 打赏
  • 举报
回复
基类如果private成员,它应该提供public或者protected接口让你设置值,这样你在operator=内调用这个函数不就可以了?
合理的方法是:基类自己也定义operator=(如果这个类是你自己定义,而你要实现拷贝,你永远要实现拷贝构造和operator=),在这种情况下,你直接调用基类operator=即可
joly_zl 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 fairchild811 的回复:]
没有指针变量,应该不存在深度copy吧
[/Quote]
不是copy的问题是,"="重载的问题。
joly_zl 2009-02-19
  • 打赏
  • 举报
回复
vehicle::operator =(cc);//调用基类的重载操作符,因为cc换个对象一定含有基类私有成员的值,但是不知道你这么做有和意义,基类的私有成员虽然被派生类集成了,但是是不可访问的,意义何在?

c.setMaxSpeed(12);
c.setMaxWeight(33);
c.setNum(11);
c1=c;的时候
c1.num也等于c.num;

fairchild811 2009-02-19
  • 打赏
  • 举报
回复
没有指针变量,应该不存在深度copy吧
s79947171 2009-02-19
  • 打赏
  • 举报
回复
上面忘记写了,派生类重载=操作符的时候也要检查是否自赋值 ,不然当数据中有需要动态开辟内存的数据在调用的时候会出错误的

if(this==&cc)//判断是否为自己赋值
{
return *this;
}
joly_zl 2009-02-19
  • 打赏
  • 举报
回复
问题解决了:
总结一下:
父类的private成员最好是用父类的接口来copy
#include <iostream>
using namespace std;

class vehicle
{
public:
vehicle()
{
mMaxSpeed=0;mMaxWeight=0;num=0;
cout<<"vehicle constructor"<<endl;
}
vehicle(int sp,int we,int n)
{
mMaxSpeed=sp;mMaxWeight=we;num=n;

}
~vehicle(){cout<<"vehicle unconstructor"<<endl;}

vehicle(const vehicle& vv)
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;

cout<<"vehicle copy-constructor"<<endl;
}

vehicle& operator =(const vehicle& vv)//"="重载
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
return *this;
}

virtual int getMaxSpeed(){cout<<"get Maxspeed vehicle\n";return mMaxSpeed;}
virtual int getMaxWeight(){cout<<"get MaxWeight vehicle\n";return mMaxWeight;}
virtual void setNum(int n){cout<<"set num vehicle\n";num=n;}
virtual void setNum(const vehicle& v){cout<<"set num vehicle\n";num=v.num;}

virtual void setMaxSpeed(int speed){cout<<"set Maxspeed vehicle\n"; mMaxSpeed=speed;}
virtual void setMaxWeight(int weight){cout<<"set MaxWeight vehicle\n"; mMaxWeight=weight;}

protected:
int mMaxSpeed;
int mMaxWeight;
private:
int num;
private:
void do_copy(const vehicle& vv)
{
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
}
};

class car :public vehicle
{
public:
car()
{
type=0;
cout<<"car constructor\n";
}
car(const car& cc)
{
setNum(cc);
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;

type=cc.type;
cout<<"car copy constructor!"<<endl;

}
car& operator =(const car& cc)//如何实现vehicle类中 的private 变量num赋值?????
{
//
setNum(cc);
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;
type=cc.type;
return *this;
}
private:
int type;
};

int main()
{
//test vehicle the base class;
vehicle v;
vehicle v1(v);
vehicle v2;
v2.setMaxSpeed(33);
v2.setMaxWeight(55);
vehicle v3=v2;
vehicle v4(v2);
int speed=v2.getMaxSpeed();
int weight=v2.getMaxWeight();
v=v2;
//////////////////////test over!
car c;
car c1;
c.setMaxSpeed(12);
c.setMaxWeight(33);
c.setNum(11);

c1=c;
car c2(c);
car c3=c;
car c4;
c4=c;

}
fairchild811 2009-02-19
  • 打赏
  • 举报
回复
sorry,是或者
fairchild811 2009-02-19
  • 打赏
  • 举报
回复
子类可以给父类private赋值,活着public或者通过函数
s79947171 2009-02-19
  • 打赏
  • 举报
回复

//基类的
vehicle& operator =(const vehicle& vv)//"="重载
{
if(this==&vv)//判断是否为自己赋值
{
return *this;
}
num=vv.num;
mMaxSpeed=vv.mMaxSpeed;
mMaxWeight=vv.mMaxWeight;
return *this;
}
//派生的
car& operator =(const car& cc)
{
vehicle::operator =(cc);//调用基类的重载操作符,因为cc换个对象一定含有基类私有成员的值,但是不知道你这么做有和意义,基类的私有成员虽然被派生类集成了,但是是不可访问的,意义何在?
mMaxSpeed=cc.mMaxSpeed;
mMaxWeight=cc.mMaxWeight;
type=cc.type;
return *this;
}
//最后就是如果成员中没有动态申请空间的数据,完全没有必要定义深度拷贝构造函数和重载=操作符
//一个类如果开始什么都没有,编译器会自动生成
1 默认构造函数 2 默认析构函数 3 默认重载=操作符 4 默认生成拷贝构造函数 5 默认当前对象地址this

上面是我的理解,如果错了,请别介意,技术问题大家多多探讨
joly_zl 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fallening 的回复:]
C/C++ codeclass C
{
private:
void do_copy( const C& c );//just make this one perfect
......
public:
C(){}
C(const C& c){do_copy(c);}
C& operator = ( const C& c){do_copy(c);}
......
};
[/Quote]
void do_copy( const C& c ) 如果C为子类,那么这个函数能给他的父类的private成员赋值?
joly_zl 2009-02-19
  • 打赏
  • 举报
回复
我错了,不是copy,
是子类的“=”重载是也要给基类的private成员赋值
tangshuiling 2009-02-19
  • 打赏
  • 举报
回复

不存在指针类成员,无需实现所谓的深COPY,换句话说vehicle v3=v2; v3与v2没有彼此依存的关系
fallening 2009-02-19
  • 打赏
  • 举报
回复
class C
{
private:
void do_copy( const C& c );//just make this one perfect
......
public:
C(){}
C(const C& c){operator=(c);}
C& operator = ( const C& c){do_copy(c); return *this;}
......
};
fallening 2009-02-19
  • 打赏
  • 举报
回复
class C
{
private:
void do_copy( const C& c );//just make this one perfect
......
public:
C(){}
C(const C& c){do_copy(c);}
C& operator = ( const C& c){do_copy(c);}
......
};
fallening 2009-02-19
  • 打赏
  • 举报
回复
class C
{
private:
void do_copy( const C& c );//just make this one perfect
......
public:
C(){}
C(const C& c){do_copy(c);}
C& operator = ( const C& c){do_copy(c);}
......
};
joly_zl 2009-02-19
  • 打赏
  • 举报
回复
help

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧