重载(一),复制构造函数

本文围绕C++编程展开,介绍了对象初始化的方法,指出在初始化阶段初始化数据成员更清晰高效,部分成员如引用和常数必须初始化。还阐述了复制构造函数,默认的是浅复制,若对象含堆中分配的成员变量,需自行实现深复制构造函数,并给出示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

初始化对象

None.gifCat(): 
None.gifitsAge(
5),
None.gifitsWeight(
8)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{ }

在初始化阶段初始化数据成员通常更清晰,效率更高。
有些数据成员必须初始化,不能在构造函数体内赋值。包括引用和常数。
只要有可能就应该使用初始化的语法。

复制构造函数(Copy Constructor)

复制构造函数的参数只有一个,即同一类的对象引用。通常应该是常数引用,因为构造函数不改变传递进来的对象。

None.gifCAT (const CAT& theCat);

默认的复制构造函数是浅复制。
如果对象中含有在堆中分配的成员变量,则默认的复制构造函数只会复制其指针,当一个副本被删除时,另一个副本的变量却仍然指向堆中的那块内存,这样可能会使程序崩溃。
因此,通常需要自己实现复制构造函数。(进行深复制)

None.gif#include <iostream>
None.gif
using namespace std;
None.gif
None.gif
class CAT
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    CAT();
InBlock.gif    CAT(
const CAT&); // copy constructor
InBlock.gif
    ~CAT();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
int GetAge() const dot.gif{return *itsAge;}
ExpandedSubBlockStart.gifContractedSubBlock.gif    
int GetWeight() const dot.gif{return *itsWeight;}
ExpandedSubBlockStart.gifContractedSubBlock.gif    
void SetAge(int age) dot.gif{*itsAge = age;}
InBlock.gif
InBlock.gif
private:
InBlock.gif    
int* itsAge;
InBlock.gif    
int* itsWeight;
ExpandedBlockEnd.gif}
;
None.gif
ExpandedBlockStart.gifContractedBlock.gifCAT::CAT() 
dot.gif{
InBlock.gif    itsAge 
= new int;
InBlock.gif    itsWeight 
= new int;
InBlock.gif    
*itsAge = 5;
InBlock.gif    
*itsWeight = 9;
ExpandedBlockEnd.gif}

None.gif
None.gif
// rhs 在 C++ 中常用来表示复制构造函数的参数。 rhs = 右端(right-hand side)
ExpandedBlockStart.gifContractedBlock.gif
CAT::CAT(const CAT& rhs) dot.gif{
InBlock.gif    itsAge 
= new int;
InBlock.gif    itsWeight 
= new int;
InBlock.gif    
*itsAge = rhs.GetAge();
InBlock.gif    
*itsWeight = rhs.GetWeight();
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifCAT::
~CAT() dot.gif{
InBlock.gif    delete itsAge;
InBlock.gif    itsAge 
= 0;
InBlock.gif    delete itsWeight;
InBlock.gif    itsWeight 
= 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
int main(int argc, char *argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    CAT frisky;
InBlock.gif    cout 
<< "frisky's age: " << frisky.GetAge() << endl;
InBlock.gif    cout 
<< "Setting frisky to 6dot.gif\n";
InBlock.gif    frisky.SetAge(
6);
InBlock.gif    cout 
<< "Creating boots from frisky\n";
InBlock.gif    CAT boots(frisky);
InBlock.gif    cout 
<< "frisky's age: " << frisky.GetAge() << endl;
InBlock.gif    cout 
<< "boots' age: " << boots.GetAge() << endl;
InBlock.gif    cout 
<< "Setting frisky to 7dot.gif\n";
InBlock.gif    frisky.SetAge(
7);
InBlock.gif    cout 
<< "frisky's age: " << frisky.GetAge() << endl;
InBlock.gif    cout 
<< "boots' age: " << boots.GetAge() << endl;
InBlock.gif     
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值