转自 http://www.jb51.net/article/37527.htm,感谢作者

#include "stdafx.h"  
    #include <iostream>  
    using namespace std;  
    class B  
    {  
    public:  
        B():data(0)    //默认构造函数  
        {   
            cout << "Default constructor is called." << endl;  
        }  
        B(int i):data(i) //带参数的构造函数  
        {  
            cout << "Constructor is called." << data << endl;  
        }  
        B(B &b)   // 复制(拷贝)构造函数  
        {  
            data = b.data; cout << "Copy Constructor is called." << data << endl;  
        }  
        B& operator = (const B &b) //赋值运算符的重载  
        {  
            this->data = b.data;  
            cout << "The operator \"= \" is called." << data << endl;  
            return *this;  
        }  
        ~B() //析构函数  
        {  
            cout << "Destructor is called. " << data << endl;  
        }  
    private:  
        int data;  
    };

//函数,参数是一个B类型对象,返回值也是一个B类型的对象  
    B fun(B b)  
    {  
        return b;  
    }

//测试函数  
    int _tmain(int argc, _TCHAR* argv[])  
    {  
        fun(1);  
        cout << endl;

B t1 = fun(2);  
        cout << endl;

B t2;  
        t2 = fun(3);

return 0;  
    }

Constructor is called.1             //用1构造参数b     
    Copy Constructor is called.1      //用b拷贝构造一个临时对象,因为此时没有对象来接受fun的返回值     
    Destructor is called. 1            //参数b被析构     
    Destructor is called. 1             //临时对象被析构  
    Constructor is called.2                  //用2构造参数b        
    Copy Constructor is called.2           //用b拷贝构造t1,此时调用的是拷贝构造函数     
    Destructor is called. 2                  //参数b被析构  
    Default constructor is called.             //调用默认的构造函数构造t2        
    Constructor is called.3                       //用3构造参数b        
    Copy Constructor is called.3             //用b拷贝构造一个临时对象        
    Destructor is called. 3                        //参数b被析构        
    The operator "= " is called.3              //调用=操作符初始化t2,此时调用的是赋值操作符     
    Destructor is called. 3                         //临时对象被析构        
    Destructor is called. 3                         //t2被析构        
    Destructor is called. 2                         //t1被析构        
    请按任意键继续. . .

Breakpoint 2, B::B (this=0x7fffffffde80, i=1) at main.cpp:11
11 B(int i):data(i) //带参数的构造函数
(gdb) s
13 cout << "Constructor is called." << data << endl;
(gdb)
Constructor is called.1
14 }
(gdb)

Breakpoint 7, main () at main.cpp:40
40 fun(b);
(gdb)

Breakpoint 4, B::B (this=0x7fffffffde90, b=...) at main.cpp:17
17 data = b.data; cout << "Copy Constructor is called." << data << endl;
(gdb)
Copy Constructor is called.1
18 }
(gdb)

Breakpoint 5, fun (b=...) at main.cpp:36
36 return b;
(gdb)

Breakpoint 4, B::B (this=0x7fffffffdea0, b=...) at main.cpp:17
17 data = b.data; cout << "Copy Constructor is called." << data << endl;
(gdb)
Copy Constructor is called.1
18 }
(gdb)
fun (b=...) at main.cpp:37
37 }
(gdb)
B::~B (this=0x7fffffffdea0, __in_chrg=<optimized out>) at main.cpp:27
27 cout << "Destructor is called. " << data << endl;
(gdb)
Destructor is called. 1
28 }
(gdb)
B::~B (this=0x7fffffffde90, __in_chrg=<optimized out>) at main.cpp:27
27 cout << "Destructor is called. " << data << endl;
(gdb)
Destructor is called. 1
28 }
(gdb)
main () at main.cpp:41
41 return 0;
(gdb)

(gdb)
Hello World!
(gdb)
B::~B (this=0x7fffffffde80, __in_chrg=<optimized out>) at main.cpp:27
27 cout << "Destructor is called. " << data << endl;
(gdb)
Destructor is called. 1
28 }
(gdb)
main () at main.cpp:44

深入C++中构造函数、拷贝构造函数、赋值操作符、析构函数的调用过程总结的更多相关文章

  1. C++中复制构造函数与重载赋值操作符总结

    前言 这篇文章将对C++中复制构造函数和重载赋值操作符进行总结,包括以下内容: 1.复制构造函数和重载赋值操作符的定义: 2.复制构造函数和重载赋值操作符的调用时机: 3.复制构造函数和重载赋值操作符 ...

  2. C++中复制构造函数与重载赋值操作符

    我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成这样的四个函数.例如以下类:   class CTe ...

  3. C++11六大函数(构造函数,移动构造函数,移动赋值操作符,复制构造函数,赋值操作符,析构函数)

    在C++中,有三大函数复制控制(复制构造函数,赋值操作符,析构函数),而在C++11中,加入了移动构造函数,移动赋值操作符.我就斗胆将他们命名为六大函数好了. 一.构造函数 c++primer中说过: ...

  4. 关于C++中的拷贝构造函数和赋值函数

    如果类定义的数据成员中存在指针或引用,那么最好重载这两个函数. 1.     定义 拷贝构造函数的定义格式:构造函数名(const 源类名& 引用对象形参名){} 赋值函数定义格式:源类名 & ...

  5. C/C++中的拷贝构造函数和赋值构造函数

    代码: #include <iostream> #include <cstdio> using namespace std; class A{ public: A(){ cou ...

  6. C++中的拷贝构造函数

    一.拷贝构造函数: 格式: A(const  A& a);  总结: 系统为对象B分配了内存并完成了与对象testA的复制过程,就类对象而言,相同类型的类对象是通过拷贝构造函数来完成整个复制过 ...

  7. 【c++ primer, 5e】构造函数 & 拷贝、赋值和析构

    [构造函数] 1.构造器就是创建对象时被调用的代码. 2.如果没有自定义构造器,那么编译器将自动合成一个默认的无参构造器. 3.自定义的构造器不允许加const,所创建const的对象只有在构造器代码 ...

  8. C++构造函数 & 拷贝构造函数 & 派生类的构造函数 & 虚继承的构造函数

    构造函数 ,是一种特殊的方法 .主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 .特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数 ...

  9. C++中复制构造函数和赋值操作符

    先看一个例子: 定义了一个类:

随机推荐

  1. 看看这些JavaScript题目你会做吗?

    题目1 咋一看这题目,还以为答案选择B呢,其实正确答案为D,知道原因吗?接着往下看 map对数组的每个元素调用定义的回调函数并返回包含结果的数组,咋一看还以为它会像如下这样执行: function t ...

  2. 浅谈datagrid详细操作单元格样式

    http://www.easyui.info/archives/470.html 今天有朋友问到:“如果设置列标题居中而列内容居右显示?”,仔细查了一下api,目前版本提供了两个相关的列属性,alig ...

  3. JDK安装成功了,环境变量也配置好了,测试代码也可以运行,但是打不开eclipse

    解决办法:删除eclipse,重新解压后,将JDK文件夹下的jre文件夹拷贝到eclipse文件夹下,OK

  4. 改变字典内的value

    import re,os limit = "8000" username = "liuhuihuang" with open("users_dict& ...

  5. C#实现AES加解密方法

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...

  6. cad2013

    ## ribbon界面? ribbon界面是一种设计ui, 可以认为是传统的  菜单和工具栏  组合. 是 用于 实时显示 + 面向结果的 设计ui 但并不是所有的程序都适合.  ribbon 并不是 ...

  7. 如何调试R程序(转载)

    R语言的调试重要性不言而喻,这段时间准备改进一个R的包,但由于接触R时间不长,中间的很多东西不懂,需要重新打包调试,以对里面的很多程序有深入了解,下面从几个方面分享一下我的收获. 1.准备工作 a)R ...

  8. ASP.NET 生成二维码(采用ThoughtWorks.QRCode和QrCode.Net两种方式)

    最近做项目遇到生成二维码的问题,发现网上用的最多的是ThoughtWorks.QRCode和QrCode.Net两种方式.访问官网看着例子写了两个Demo,使用过程中发现两个都挺好用的,Thought ...

  9. HttpWebRequest后台读取网页类

    using System;using System.Linq;using System.Collections.Generic;using System.Web;using System.Config ...

  10. linux服务器部署svn常见错误处理→转载

    转载地址→http://blog.seweal.com/post/2013-02-04/svn-errors [开放svn端口] iptables -I INPUT -p tcp --dport 36 ...