c++類對象的生存週期 二

構造了一個Test的類

代碼如下:

//
// Created by yanpan on 2019/4/21.
//

#if 1
#include <iostream>
using namespace std;

class Test
{
public:
    Test(int a = 10, int b = 10):_a(a),_b(b)  //帶有默認值的構造函數
    {
        cout << this << endl;
        cout << "a: " << _a << " b: " << _b << endl;
        cout << "test()" << endl;
    };
    Test(const Test &src)         //拷貝構造函數
    {
        _a = src._a;
        _b = src._b;
        cout << &src << "->" << this << endl;
        cout << "a: " << _a << " b: " << _b << endl;
        cout << "test(const test &src)" << endl;
    }
    void operator=(const Test &src)   //賦值運算符重載函數
    {
        _a = src._a;
        _b = src._b;
        cout << &src << "->" << this << endl;
        cout << "a: " << _a << " b: " << _b << endl;
        cout << "operator=(const test &src))" << endl;
    }
    ~Test()              //析構函數
    {
        cout << this << endl;
        cout << "a: " << _a << " b: " << _b << endl;
        cout << "~Test()" << endl;
    }
private:
    int _a;
    int _b;
};

Test test1(20,10);
int main()
{
    Test test2(20, 20);
    Test test3 = test2;
    static Test test4 = Test(40, 40);//初始化的靜態局部變量在.data段存儲,程序的存活週期就是靜態變量的生存週期
    test2 = Test(50, 50);
    test2 = (Test)(60, 60);
    test2 = 70;         //隱式生成臨時對象
    Test *p1 = &Test(80);     //用指針指向臨時對象,出了語句臨時對象會析構
    Test &q1 = Test(90, 50);   //引用變量引用臨時對象,臨時對象的生存週期和引用變量的生存週期一樣
    Test *p2 = new Test;
    delete p2;
    return 0;
}
Test test5(100, 100);
#endif
  • 調用構造函數 構造test1
  • 調用構造函數 構造test5
  • 調用構造函數 構造test2
  • 調用拷貝構造函數 拷貝構造test3
  • 調用構造函數構造靜態局部對象test4(test4的存儲在.data段)
  • 調用構造函數構造Test(50, 50)臨時對象, 將臨時對象賦值給test2,臨時對象析構
  • 調用構造函數構造Test(60, 10)臨時對象 ps:逗號表達式瞭解一下, 將臨時對象賦值給test2,析構臨時對象
  • 調用構造函數構造Test(70, 10)臨時對象  隱式生成臨時對象 是常對象 ps:賦值重載函數的參數設置爲const的原因之一,將臨時對象賦值給test2,析構臨時對象
  • 調用構造函數構造Test(80, 10)臨時對象 用指針p1指向臨時對象,但臨時對象除了語句就會析構,所以除了函數調用的情況外,不要用指針指向臨時對象;
  • 調用構造函數構造Test(90, 50)臨時對象  用引用變量q1引用生成的臨時對象,臨時對象出了語句不析構,和引用變量的生存週期一樣
  • 用指針p2指向在堆上調用構造函數構造一個堆上的對象, 調用delete 則調用析構函數析構堆上的對象,釋放內存
  • 結下調用析構函數,依次析構q1引用的臨時對象、析構test3、析構test2、析構test4、析構test5、析構test1

運行結果

00190218
a: 20 b: 10
test()
00190220
a: 100 b: 100
test()
00CFFC48
a: 20 b: 20
test()
00CFFC48->00CFFC38
a: 20 b: 20
test(const test &src)
0019022C
a: 40 b: 40
test()
00CFFAC8
a: 50 b: 50
test()
00CFFAC8->00CFFC48
a: 50 b: 50
operator=(const test &src))
00CFFAC8
a: 50 b: 50
~Test()
00CFFAD8
a: 60 b: 10
test()
00CFFAD8->00CFFC48
a: 60 b: 10
operator=(const test &src))
00CFFAD8
a: 60 b: 10
~Test()
00CFFAE8
a: 70 b: 10
test()
00CFFAE8->00CFFC48
a: 70 b: 10
operator=(const test &src))
00CFFAE8
a: 70 b: 10
~Test()
00CFFAF8
a: 80 b: 10
test()
00CFFAF8
a: 80 b: 10
~Test()
00CFFC10
a: 90 b: 50
test()
0101DF50
a: 10 b: 10
test()
0101DF50
a: 10 b: 10
~Test()
00CFFC10
a: 90 b: 50
~Test()
00CFFC38
a: 20 b: 20
~Test()
00CFFC48
a: 70 b: 10
~Test()
0019022C
a: 40 b: 40
~Test()
00190220
a: 100 b: 100
~Test()
00190218
a: 20 b: 10
~Test()
請按任意鍵繼續. . .

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章