[問題] copy constructor and operator= 的覆寫
一般書上說當你的class有含指標時,要自己寫copy constructor以及覆寫operator=
如果我一個class像這樣:
class A{
int* ptr;
double x;
int y;
.....
.....
A( const A& rhd );
A& operator=( const A& rhd );
////....尚有一堆data member..... ////
};
那我要另外寫一個copy constructor:
A::A( const A& rhd ){
this->ptr = new int(0);
*this->ptr = *rhd.ptr;
this->x = rhd.x;
this->y = rhd.y;
....
....
//// .......... 其他data member也要複製....../////
}
//// operator= 複寫也一樣 ////
我的問題是如果data member有很多,那我一個一個寫在copy constructor內不就很笨?
有更快的或更方便的作法嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.132.128.158
→
03/06 19:40, , 1F
03/06 19:40, 1F
推
03/06 19:41, , 2F
03/06 19:41, 2F
→
03/06 19:41, , 3F
03/06 19:41, 3F
→
03/06 19:43, , 4F
03/06 19:43, 4F
→
03/06 19:43, , 5F
03/06 19:43, 5F
→
03/06 20:33, , 6F
03/06 20:33, 6F
推
03/06 20:57, , 7F
03/06 20:57, 7F
→
03/06 21:06, , 8F
03/06 21:06, 8F
→
03/07 04:45, , 9F
03/07 04:45, 9F
推
03/07 10:58, , 10F
03/07 10:58, 10F
推
03/09 12:29, , 11F
03/09 12:29, 11F
→
03/09 12:30, , 12F
03/09 12:30, 12F
→
03/09 12:31, , 13F
03/09 12:31, 13F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章