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;
}

返回值为对象调用拷贝构造函数相关推荐

  1. 函数返回类的对象与拷贝构造函数

    C++中,如果我们在一个函数中,定义了一个类的对象,然后返回这个对象,在main函数中用一个对象去接受这个返回的对象的时候,这里面参与的函数调用大家可能不熟悉,这里通过程序和注释的方式给大家讲解一下. ...

  2. C++基础知识 - 什么时候调用拷贝构造函数

    什么时候调用拷贝构造函数 1. 调用函数时,实参是对象,形参不是引用类型 如果函数的形参是引用类型,就不会调用拷贝构造函数 #include "Human.h" using nam ...

  3. 何时会调用拷贝构造函数

    拷贝构造函数什么时候用到 当用一个已初始化过了的自定义类类型对象去初始化另一个新构造的对象的时候,拷贝构造函数就会被自动调用.也就是说,当类的对象需要拷贝时,拷贝构造函数将会被调用.以下情况都会调用拷 ...

  4. C++类对象的拷贝构造函数(转载)

    对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a=100; int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象拷贝的简单例子. ...

  5. 自动调用拷贝构造函数的三种情况

    自动调用拷贝构造函数的三种情况 首先介绍拷贝构造函数的定义形式: class 类名 { public: 构造函数名称 (类名 &变量名) { 函数体 } -- }; 拷贝构造函数是使用类对象的 ...

  6. java分布式对象——远程方法中的参数和返回值+远程对象激活

    [0]README 1)本文文字描述转自 core java volume 2, 旨在学习 java分布式对象--远程方法中的参数和返回值+远程对象激活 的相关知识: [1]远程方法中的参数和返回值 ...

  7. boost::signals2模块实现用于从槽返回值到信号调用的示例程序

    boost::signals2模块实现用于从槽返回值到信号调用的示例程序 实现功能 C++实现代码 实现功能 boost::signals2模块实现用于从槽返回值到信号调用的示例程序 C++实现代码 ...

  8. oracle java存储过程返回值_java程序调用Oracle 存储过程 获取返回值(无返回,非结果集,结果集)...

    java程序调用Oracle 存储过程 获取返回值(无返回,非结 果集,结果集) oracle中procedure是不能有返回值的,要想返回值,就得有 输出参数,同样要想返回记录集,可以把游标类型作为 ...

  9. C++学习笔记-----在重载的赋值运算函数中调用拷贝构造函数

    类的拷贝构造函数与赋值运算不同,拷贝构造函数是对这个类进行初始化的过程,而赋值是删除原有的东西,赋予它新的东西. 但是二者在实现上是互通的. template<class T> graph ...

  10. xhr返回值_XHR对象

    一.XMLHttpRequest对象 var xhr = newXMLHttpRequest(), i= 0;for(var key inxhr){if(xhr.hasOwnProperty(key) ...

最新文章

  1. 使用version遇到的那些坑
  2. Linux下的示例程序
  3. springboot学习笔记一(从maven项目到springboot)
  4. 如何用c语言验证一个定理,验证动量定理方法一
  5. 这些数学趣图,数学老师看了后会怎么想?
  6. python php multiprocessing,Python多进程并发(multiprocessing)用法实例详解
  7. Spring中的@Value注解详解
  8. cdr非法软件 您的产品已被禁用怎么回事_多层刺网非法捕鱼 顺庆男子被取保候审...
  9. 他开发了 redux,昨晚字节一面却挂了?
  10. python 计算协方差_Python3Numpy——相关性协方差应用
  11. Atitit java rest mvc微服务原理以及框架选型 目录 第一节 Mvc原理 model controler view 1 第二章 Spark 最简单 1 第一节 Sprbt to
  12. MediaCodec解析MP4视频
  13. @Transactional注解下,Mybatis循环取序列的值,但得到的值都相同的问题
  14. 大一微积分笔记整理_大一高等数学学习方法
  15. 教育研究方法 的思维导图
  16. Book04--修改软件的艺术:构建易维护代码的9条最佳实践
  17. git pull拉去不到最新代码 更新不出来代码
  18. 阵列信号处理笔记-波达方向DOA-子空间方法
  19. 原生JS【fiveKeyPress】2秒内五次点击键盘任意键(或组合键)触发自定义事件(以Pause/Break键为例)
  20. 这家安全厂商避谈“软件定义”,我却要为它的“反骨”点赞

热门文章

  1. 【转】word公式大括号左对齐
  2. 婚宴座位图html5,婚宴座位安排图 婚宴主桌安排示意图
  3. python语言程序设计教程赵璐 第三章 课后习题 程序流程控制
  4. 求斐波那契数列的三种方法
  5. 帆软报表扩展列计算同比环比
  6. Java流系列(三):数据流、缓存流
  7. 在硅谷初创公司怎么找投资人
  8. 曾经的移动应用推广八法尚能饭否?再加一法大概就事半功倍!
  9. LTE解MIB块,LTE中PBCH过程
  10. c语言水仙花数pow,c语言如何解水仙花数