返回值为对象调用拷贝构造函数



copyFrisky对象超出作用区域,
这时会调用复制构造函数创建该copyFrisky对象的一个临时拷贝对象,
并把它赋给主调函数中需要的对象,此时是成员复制;第二个例子可以证明!
然后调用该临时拷贝对象的析构函数释放这个对象占用的内存资源,
接着再调用形参对象的析构函数释放形参对象占用的内存资源!

以下是一个验证的例子:

//------------------------------------
//file name: testPlace.cpp
//purpose: to test copy constructor
//------------------------------------
#include <iostream.h>

class Cat
{
public:
Cat(); //default constructor
Cat(const Cat &);        //copy constructor
~Cat();           //destructor
int GetAge() const {return itsAge;}
int GetWeight() const {return itsWeight;}
void SetAge(int age) {itsAge=age;}
void SetWeight(int weight) {itsWeight=weight;}
private:
int itsAge;
int itsWeight;
};

Cat::Cat()          //constructor realization
{
itsAge=1;
itsWeight=10;
cout<<"constructor\n";
}

Cat::Cat(const Cat & theCat)     //copy constructor realization
{
itsAge=theCat.GetAge();
itsWeight=theCat.GetWeight();
cout<<"copy constructor\n";
}

Cat::~Cat()          //destructor realization
{
itsAge=0;
itsWeight=0;
cout<<"destructor\n";
}

Cat DisplayCopyFrisky(Cat);     //subfuction header

int main()          //main function
{
Cat Frisky,copyFrisky;
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
Frisky.SetAge(2);
copyFrisky=DisplayCopyFrisky(Frisky);     //call subfuction
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
Frisky.SetAge(3);
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
return 0;
}

Cat DisplayCopyFrisky(Cat copyFrisky) //subfuction realization
{
cout<<"copyFrisky' age: "<<copyFrisky.GetAge() <<endl;
cout<<"copyFrisky' weight: "<<copyFrisky.GetWeight() <<endl;
copyFrisky.SetAge(4);
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
return copyFrisky;
}

证明返回对象的值时是成员复制的第二个例子:由于返回对象值时采用的是浅复制,而对象中含有指向自由存储区的指针变量,从而导致临时拷贝对象中的指针变量和接受返回值的对象(在此设为:copyFrisky)的指针变量同时指向同一内存块,而当浅复制完毕后,析构函数释放临时拷贝对象的内存资源,导致接受返回值的对象copyFrisky中的指针变量变成迷途指针,返回到主程序之后,在主程序结束前,应该先把copyFrisky中的迷途指针设为空指针或者指向某一分配的内存,否则当主程序结束调用析构函数释放copyFrisky对象的资源时,将会导致错误!正如第二个例子所示!改正后的程序如第三个例子!

//------------------------------------
//file name: testPlace.cpp
//purpose: to test copy constructor
//------------------------------------
#include <iostream.h>
class Cat
{
public:
Cat();           //default constructor
Cat(const Cat &);        //copy constructor
~Cat();           //destructor
int GetAge() const {return *itsAge;}
int GetWeight() const {return *itsWeight;}
int* GetItsAgeValue() const {return itsAge;}
int* GetItsWeightValue() const {return itsWeight;}
void DeleteAge() {delete itsAge;itsAge=0;}
void DeleteWeight() {delete itsWeight;itsWeight=0;}
void SetAge(int age) {*itsAge=age;}
void SetWeight(int weight) {*itsWeight=weight;}
private:
int *itsAge;
int *itsWeight;
};

Cat::Cat()          //constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=1;
*itsWeight=10;
cout<<"constructor\n";
}

Cat::Cat(const Cat & theCat)     //copy constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=theCat.GetAge();
*itsWeight=theCat.GetWeight();
cout<<"copy constructor\n";
}

Cat::~Cat()          //destructor realization
{
delete itsAge;
itsAge=0;
delete itsWeight;
itsWeight=0;
cout<<"destructor\n";
}

Cat DisplayCopyFrisky(Cat);     //subfuction header

int main()          //main function
{
Cat Frisky,copyFrisky;
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
Frisky.SetAge(2);

copyFrisky.DeleteAge();
copyFrisky.DeleteWeight();

copyFrisky=DisplayCopyFrisky(Frisky);     //call subfuction
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;

cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;   //lost pointer
cout<<"copyFrisky's weight: "<<copyFrisky.GetWeight() <<endl; //lost pointer

Frisky.SetAge(3);
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
return 0;
}

Cat DisplayCopyFrisky(Cat copyFrisky)   //subfuction realization
{
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;
copyFrisky.SetAge(4);
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
return copyFrisky;
}

第三个例子:这个例子返回的是迷途指针,此目的只是想了解返回对象值时的一些情况!

//------------------------------------
//file name: testPlace.cpp
//purpose: to test copy constructor
//------------------------------------
#include <iostream.h>
class Cat
{
public:
Cat();           //default constructor
Cat(const Cat &);        //copy constructor
~Cat();           //destructor
int GetAge() const {return *itsAge;}
int GetWeight() const {return *itsWeight;}
int* GetItsAgeValue() const {return itsAge;}
int* GetItsWeightValue() const {return itsWeight;}
void DeleteAge() {delete itsAge;itsAge=0;}
void DeleteWeight() {delete itsWeight;itsWeight=0;}
void SetAgeEmpty() {itsAge=0;}
void SetWeightEmpty() {itsWeight=0;}
void SetAge(int age) {*itsAge=age;}
void SetWeight(int weight) {*itsWeight=weight;}
private:
int *itsAge;
int *itsWeight;
};

Cat::Cat()          //constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=1;
*itsWeight=10;
cout<<"constructor\n";
}

Cat::Cat(const Cat & theCat)     //copy constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=theCat.GetAge();
*itsWeight=theCat.GetWeight();
cout<<"copy constructor\n";
}

Cat::~Cat()          //destructor realization
{
delete itsAge;
itsAge=0;
delete itsWeight;
itsWeight=0;
cout<<"destructor\n";
}

Cat DisplayCopyFrisky(Cat);     //subfuction header

int main()          //main function
{
Cat Frisky,copyFrisky;
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
Frisky.SetAge(2);

copyFrisky.DeleteAge();
copyFrisky.DeleteWeight();
copyFrisky=DisplayCopyFrisky(Frisky);     //call subfuction

copyFrisky.SetAgeEmpty();
copyFrisky.SetWeightEmpty();

cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;

//cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;   //lost pointer
//cout<<"copyFrisky's weight: "<<copyFrisky.GetWeight() <<endl; //lost pointer

Frisky.SetAge(3);
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
return 0;
}

Cat DisplayCopyFrisky(Cat copyFrisky)   //subfuction realization
{
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;
copyFrisky.SetAge(4);
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
return copyFrisky;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第0章 导读(译者的话) 第1章 关于对象(Object Lessons) 加上封装后的布局成本(Layout Costs for Adding Encapsulation) 1.1 C++模式模式(The C++ Object Model) 简单对象模型(A Simple Object Model) 表格驱动对象模型(A Table-driven Object Model) C++对象模型(Th e C++ Object Model) 对象模型如何影响程序(How the Object Model Effects Programs) 1.2 关键词所带来的差异(A Keyword Distinction) 关键词的困扰 策略性正确的struct(The Politically Correct Struct) 1.3 对象的差异(An Object Distinction) 指针的类型(The Type of a Pointer) 加上多态之后(Adding Polymorphism) 第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual Function”的Class “带有一个virual Base class”的Class 总结 2.2 Copy Constructor的建构操作 Default Memberwise Initialization Bitwise Copy Semantics(位逐次拷贝) 不要Bitwise Copy Semantics! 重新设定的指针Virtual Table 处理Virtual Base Class Subobject 2.3 程序转换语意学(Program Transformation Semantics) 明确的初始化操作(Explicit Initialization) 参数的初始化(Argument Initialization) 返回值的初始化(Return Value Initialization) 在使用者层面做优化(Optimization at the user Level) 在编译器层面做优化(Optimization at the Compiler Level) Copy Constructor:要还是不要? 摘要 2.4 成员们的初始化队伍(Member Initialization List) 第3章 Data语意学(The Semantics of Data) 3.1 Data Member的绑定(The Binding of a Data Member) 3.2 Data Member的布局(Data Member Layout) 3.3 Data Member的存取 Static Data Members Nonstatic Data Member 3.4 “继承”与Data Member 只要继承不要多态(Inheritance without Polymorphism) 加上多态(Adding Polymorphism) 多重继承(Multiple Inheritance) 虚拟继承(Virtual Inheritance) 3.5 对象成员的效率(Object Member Efficiency) 3.6 指向Data Members的指针(Pointer to Data Members) “指向Members的指针”的效率问题 第4章 Function语意学(The Semantics of Function) 4.1 Member的各种调用方式 Nonstatic Member Functions(非静态成员函数) Virtual Member Functions(虚拟成员函数) Static Member Functions(静态成员函数) 4.2 Virtual Member Functions(虚拟成员函数) 多重继承下的Virtual Functions 虚拟继承下的Virtual Functions 4.3 函数的效能 4.4 指向Member Functions的指针(Pointer-to-Member Functions) 支持“指向Virtual Member Functions”之指针 在多重继承之下,指向Member Functions的指针 “指向Member Functions之指针”的效率 4.5 Inline Functions 形式对数(Formal Arguments) 局部变量(Local Variables) 第5章 构造、解构、拷贝 语意学(Semantics of Construction,Destruction,and Copy) 纯虚拟函数的存在(Presence of a Pure Virtual Function) 虚拟规格的存在(Presence of a Virtual Specification) 虚拟规格中const的存在 重新考虑class的声明 5.1 无继承情况下的对象构造 抽象数据类型(Abstract Data Type) 为继承做准备 5.2 继承体系下的对象构造 虚拟继承(Virtual Inheritance) 初始化语意学(The Semantics of the vptr Initialization) 5.3 对象复制语意学(Object Copy Semantics) 5.4 对象的功能(Object Efficiency) 5.5 解构语意学(Semantics of Destruction) 第6章 执行期语意学(Runting Semantics) 6.1 对象的构造和解构(Object Construction and Destruction) 全局对象(Global Objects) 局部静态对象(Local Static Objects) 对象数组(Array of Objects) Default Constructors和数组 6.2 new和delete运算符 针对数组的new语意 Placement Operator new的语意 6.3 临时性对象(Temporary Objects) 临时性对象的迷思(神话、传说) 第7章 站在对象模型的类端(On the Cusp of the Object Model) 7.1 Template Template的“具现”行为(Template Instantiation) Template的错误报告(Error Reporting within a Template) Template中的名称决议方式(Name Resolution within a Template) Member Function的具现行为(Member Function Instantiation) 7.2 异常处理(Exception Handling) Exception Handling快速检阅 对Exception Handling的支持 7.3 执行期类型识别(Runtime Type Identification,RTTI) Type-Safe Downcast(保证安全的向下转型操作) Type-Safe Dynamic Cast(保证安全的动态转型) References并不是Pointers Typeid运算符 7.4 效率有了,弹性呢? 动态共享函数库(Dynamic Shared Libraries) 共享内存(Shared Memory)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值