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

时间:2021-07-30 09:01:42

转自 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