C++構(gòu)造函數(shù)初始化順序詳解
1.構(gòu)造函數(shù)、析構(gòu)函數(shù)與拷貝構(gòu)造函數(shù)介紹
構(gòu)造函數(shù)
1.構(gòu)造函數(shù)不能有返回值
2.缺省構(gòu)造函數(shù)時,系統(tǒng)將自動調(diào)用該缺省構(gòu)造函數(shù)初始化對象,缺省構(gòu)造函數(shù)會將所有數(shù)據(jù)成員都初始化為零或空
3.創(chuàng)建一個對象時,系統(tǒng)自動調(diào)用構(gòu)造函數(shù)
析構(gòu)函數(shù)
1.析構(gòu)函數(shù)沒有參數(shù),也沒有返回值。不能重載,也就是說,一個類中只可能定義一個析構(gòu)函數(shù)
2.如果一個類中沒有定義析構(gòu)函數(shù),系統(tǒng)也會自動生成一個默認(rèn)的析構(gòu)函數(shù),為空函數(shù),什么都不做
3.調(diào)用條件:1.在函數(shù)體內(nèi)定義的對象,當(dāng)函數(shù)執(zhí)行結(jié)束時,該對象所在類的析構(gòu)函數(shù)會被自動調(diào)用;2.用new運算符動態(tài)構(gòu)建的對象,在使用delete運算符釋放它時。
拷貝構(gòu)造函數(shù)
拷貝構(gòu)造函數(shù)實際上也是構(gòu)造函數(shù),具有一般構(gòu)造函數(shù)的所有特性,其名字也與所屬類名相同??截悩?gòu)造函數(shù)中只有一個參數(shù),這個參數(shù)是對某個同類對象的引用。它在三種情況下被調(diào)用:
1.用類的一個已知的對象去初始化該類的另一個對象時;
2.函數(shù)的形參是類的對象,調(diào)用函數(shù)進(jìn)行形參和實參的結(jié)合時;
3.函數(shù)的返回值是類的對象,函數(shù)執(zhí)行完返回調(diào)用者。
【代碼】
/*
version: 1.0
author: hellogiser
date: 2014/9/25
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class point
{
private:
    int x, y;
public:
    point(int xx = 0, int yy = 0)
    {
        x = xx;
        y = yy;
        cout << "Constructor" << endl;
    }
    point(const point &p)
    {
        x = p.x;
        y = p.y;
        cout << "Copy Constructor" << endl;
    }
    ~point()
    {
        cout << "Destructor" << endl;
    }
    int get_x()
    {
        return x;
    }
    int get_y()
    {
        return y;
    }
};
void f(point p)
{
    // copy constructor
    cout << p.get_x() << "  " << p.get_y() << endl;
    // destructor
}
point g()
{
    point a(7, 33); //constructor
    return a; // copy constructor    temp object
}
void test()
{
    point a(15, 22); // constructor
    point b(a); //(1) copy constructor
    cout << b.get_x() << "  " << b.get_y() << endl; // 15 22
    f(b);//  (2) copy constructor
    b = g(); // (3) copy constructor
    cout << b.get_x() << "  " << b.get_y() << endl; // 7  33
}
int main()
{
    test();
    return 0;
}
/*
Constructor
Copy Constructor
15      22
Copy Constructor
15      22
Destructor
Constructor
Copy Constructor
Destructor
Destructor
7       33
Destructor
Destructor
*/
2. 繼承關(guān)系中構(gòu)造函數(shù)執(zhí)行順序
(1)任何虛擬基類(virtual)的構(gòu)造函數(shù)按照它們被繼承的順序構(gòu)造;
(2)任何非虛擬基類(non-virtual)的構(gòu)造函數(shù)按照它們被繼承的順序構(gòu)造;
(3)任何成員對象(data member)的構(gòu)造函數(shù)按照它們聲明的順序調(diào)用;
(4)類自己的構(gòu)造函數(shù)(self)。
【代碼】
/*
version: 1.0
author: hellogiser
date: 2014/9/27
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class OBJ1
{
public:
    OBJ1()
    {
        cout << "OBJ1\n";
    }
};
class OBJ2
{
public:
    OBJ2()
    {
        cout << "OBJ2\n";
    }
};
class Base1
{
public:
    Base1()
    {
        cout << "Base1\n";
    }
};
class Base2
{
public:
    Base2()
    {
        cout << "Base2\n";
    }
};
class Base3
{
public:
    Base3()
    {
        cout << "Base3\n";
    }
};
class Base4
{
public:
    Base4()
    {
        cout << "Base4\n";
    }
};
class Derived : public Base1, virtual public Base2,
    public Base3, virtual public Base4
{
public:
    Derived() : Base4(), Base3(), Base2(),
        Base1(), obj2(), obj1()
    {
        cout << "Derived.\n";
    }
protected:
    OBJ1 obj1;
    OBJ2 obj2;
};
void test()
{
    Derived aa;
    cout << "This is ok.\n";
}
int main()
{
    test();
    return 0;
}
/*
Base2
Base4
Base1
Base3
OBJ1
OBJ2
Derived.
This is ok.
*/
【代碼2】
/*
version: 1.0
author: hellogiser
date: 2014/9/27
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base1
{
public:
    Base1(int i)
    {
        cout << "Base1 " << i << endl;
    }
};
class Base2
{
public:
    Base2(int i)
    {
        cout << "Base2 " << i << endl;
    }
};
class Base3
{
public:
    Base3()
    {
        cout << "Base3 *" << endl;
    }
};
class Derived : public Base2,  public Base1, virtual public Base3
{
public:
    Derived(int a, int b, int c, int d, int e)
        : Base1(a), b2(d), b1(c), Base2(b)
    {
        m = e;
        cout << "Derived.\n";
    }
protected:
    Base1 b1;
    Base2 b2;
    Base3 b3;
    int m;
};
void test()
{
    Derived aa(1, 2, 3, 4, 5);
    cout << "This is ok.\n";
}
int main()
{
    test();
    return 0;
}
/*
Base3 *
Base2 2
Base1 1
Base1 3
Base2 4
Base3 *
Derived.
This is ok.
*/
分析:
(1) virtual
按照繼承順序:Base3
第一步:先繼承Base3,在初始化列表里找不到Base3(), 則調(diào)用Base3里的默認(rèn)構(gòu)造函數(shù)Base3(),打印"Base3 *"
(2)non-virtual
按照繼承順序:Base2,Base1
第二步:繼承Base2,在初始化列表中找Base2(b),調(diào)用Base2的構(gòu)造函數(shù)Base2(2),打印"Base2 2"
第三步:繼承Base1,在初始化列表中找Base1(a),調(diào)用Base1的構(gòu)造函數(shù)Base1(1),打印"Base1 1"
(3)data member
按照申明順序:b1,b2,b3
第四步:構(gòu)造b1,在初始化列表中找b1(c),調(diào)用Base1的構(gòu)造函數(shù)Base1(3),打印"Base1 3"
第五步:構(gòu)造b2,在初始化列表中找b2(d),調(diào)用Base2的構(gòu)造函數(shù)Base1(4),打印"Base2 4"
第六步:構(gòu)造b3,在初始化列表中找不到b3(),調(diào)用Base3的構(gòu)造函數(shù)Base3(),打印"Base3 *"
(4)self
第7步:執(zhí)行自己的構(gòu)造函數(shù)體,輸出"Derived."
上一篇:C++常用字符串分割方法實例匯總
欄 目:C語言
本文標(biāo)題:C++構(gòu)造函數(shù)初始化順序詳解
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3320.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10delphi制作wav文件的方法
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 01-10C#中split用法實例總結(jié)
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 


