图示构造函数的析构顺序(默认构造函数、拷贝构造函数和赋值运算符重载函数)

示例代码:

#include
using namespace std;

class Test
{
public:
    Test(int a=10,int b=20)
    {
        _a = a;
        _b = b;
        cout << this << " ";
        cout << "Test(int,int)" << endl;
        cout << "_a = " << _a << ",_b = " << _b << endl; 
    }
    Test(const Test& src)
    {
        _a = src._a;
        cout << this << " ";
        cout << "Test(const Test&)" << endl;
        cout << "_a = " << _a << ",_b = " << _b << endl; 
    }
    void operator=(const Test& src)
    {
        _a = src._a;
        cout << this << " ";
        cout << "operator(const Test&)" << endl;
        cout << "_a = " << _a << ",_b = " << _b << endl; 
    }
    ~Test()
    {
        cout << this << " ";
        cout << "~Test()" << endl;
        cout << "_a = " << _a << ",_b = " << _b << endl; 
    }
private:
    int _a;
    int _b;
};


Test t1(20,20);
int main()
{
    Test t2(30,30);
    Test t3 = t2;
    static Test t4 = Test(40,40); 
    t2 = Test(50,50);
    t2 = (Test)(60,60);
    t2 = 70;
    Test *p1 = &Test(70,70);
    Test &q = Test(80,80);
    Test *p2 = new Test;
    delete p2; 
    return 0;
}
Test t5(90,90);

运行结构分析:

图示构造函数的析构顺序(默认构造函数、拷贝构造函数和赋值运算符重载函数)_第1张图片

你可能感兴趣的:(c/c++)