C++派生类与基类构造函数调用次序
生活随笔
收集整理的這篇文章主要介紹了
C++派生类与基类构造函数调用次序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文用來測試C++基類和派生類構造函數,析構函數,和拷貝構造函數的調用次序。
運行環境:SUSE Linux Enterprise Server 11 SP2 ?(x86_64)?
#include <iostream>
using namespace std;
class Base
{
public:
? ? Base()
? ? {
? ? ? ? cout << "Base Constructor" << std::endl;
? ? }
? ? Base(const Base& other)
? ? {
? ? ? ? cout << "Base Copy Constructor" << std::endl;
? ? }
? ? virtual ~Base()
? ? {
? ? ? ? cout << "Base Destructor" << std::endl;
? ? }
? ? const Base & operator = (const ?Base& other)
? ? {
? ? ? ? cout << "assignment operator" << std::endl;
? ? }
};
class Derived: public Base
{
public:
? ? Derived()
? ? {
? ? ? ? cout << "Derived Constructor" << std::endl;
? ? }
? ? Derived(const Derived& other)
? ? {
? ? ? ? cout << "Derived Copy Constructor" << std::endl;
? ? }
? ? virtual ~Derived()
? ? {
? ? ? ? cout << "Derived Destructor" << std::endl;
? ? }
? ? const Derived & operator = (const ?Derived& other)
? ? {
? ? ? ? cout << "assignment operator" << std::endl;
? ? }
};
==============================================================
A. 測試派生類對象
int main(int argc, char *argv[])
{
? ? Derived d1;
? ? return 1;
}
輸出:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
結論:?
1.先構造父類,才構造子類,沒有父親,哪有兒子啊!
2.即使子類(派生類)沒有顯式(explicit)的調用(在初始化列表中調用)父類(基類)的構造函數,父類的構造函數也會被調用;
================================================================
B. 測試基類的拷貝構造函數
int main(int argc, char *argv[])
{
? ? Base b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Base b2 = b1;
? ? cout << "b2 constructed done" << ?std::endl;
? ? Base b3(b2);
? ? cout << "b3 constructed done" << ?std::endl;
? ? return 1;
}
輸出結果:
Base Constructor
b1 constructed done
Base Copy Constructor
b2 constructed done
Base Copy Constructor
b3 constructed done
Base Destructor
Base Destructor
Base Destructor
結論:
1. 代碼: "Base b2 = b1;" 和 "Base b3(b2);"
將調用拷貝構造函數,而不是其它的(賦值、構造)函數;
擴展: 函數參數傳值調用也會調用拷貝構造函數
添加函數:
void Func(Base b)
{
}
int main(int argc, char *argv[])
{
? ? Base b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Func(b1);
? ? return 1;
}
輸出結果:
Base Constructor
b1 constructed done
Base Copy Constructor
Base Destructor
Base Destructor
可見: 函數參數傳值調用也會調用拷貝構造函數
==================================================================
C. 測試派生類的拷貝構造函數
int main(int argc, char *argv[])
{
? ? Derived b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Derived b2 = b1;
? ? cout << "b2 constructed done" << ?std::endl;
? ? Derived b3(b2);
? ? cout << "b3 constructed done" << ?std::endl;
? ? return 1;
}
輸出結果:
Base Constructor
Derived Constructor
b1 constructed done
Base Constructor
Derived Copy Constructor
b2 constructed done
Base Constructor
Derived Copy Constructor
b3 constructed done
Derived Destructor
Base Destructor
Derived Destructor
Base Destructor
Derived Destructor
Base Destructor
結論:
1. 派生類的拷貝構造函數被調用前,會調用子類的構造函數;
運行環境:SUSE Linux Enterprise Server 11 SP2 ?(x86_64)?
#include <iostream>
using namespace std;
class Base
{
public:
? ? Base()
? ? {
? ? ? ? cout << "Base Constructor" << std::endl;
? ? }
? ? Base(const Base& other)
? ? {
? ? ? ? cout << "Base Copy Constructor" << std::endl;
? ? }
? ? virtual ~Base()
? ? {
? ? ? ? cout << "Base Destructor" << std::endl;
? ? }
? ? const Base & operator = (const ?Base& other)
? ? {
? ? ? ? cout << "assignment operator" << std::endl;
? ? }
};
class Derived: public Base
{
public:
? ? Derived()
? ? {
? ? ? ? cout << "Derived Constructor" << std::endl;
? ? }
? ? Derived(const Derived& other)
? ? {
? ? ? ? cout << "Derived Copy Constructor" << std::endl;
? ? }
? ? virtual ~Derived()
? ? {
? ? ? ? cout << "Derived Destructor" << std::endl;
? ? }
? ? const Derived & operator = (const ?Derived& other)
? ? {
? ? ? ? cout << "assignment operator" << std::endl;
? ? }
};
==============================================================
A. 測試派生類對象
int main(int argc, char *argv[])
{
? ? Derived d1;
? ? return 1;
}
輸出:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
結論:?
1.先構造父類,才構造子類,沒有父親,哪有兒子啊!
2.即使子類(派生類)沒有顯式(explicit)的調用(在初始化列表中調用)父類(基類)的構造函數,父類的構造函數也會被調用;
================================================================
B. 測試基類的拷貝構造函數
int main(int argc, char *argv[])
{
? ? Base b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Base b2 = b1;
? ? cout << "b2 constructed done" << ?std::endl;
? ? Base b3(b2);
? ? cout << "b3 constructed done" << ?std::endl;
? ? return 1;
}
輸出結果:
Base Constructor
b1 constructed done
Base Copy Constructor
b2 constructed done
Base Copy Constructor
b3 constructed done
Base Destructor
Base Destructor
Base Destructor
結論:
1. 代碼: "Base b2 = b1;" 和 "Base b3(b2);"
將調用拷貝構造函數,而不是其它的(賦值、構造)函數;
擴展: 函數參數傳值調用也會調用拷貝構造函數
添加函數:
void Func(Base b)
{
}
int main(int argc, char *argv[])
{
? ? Base b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Func(b1);
? ? return 1;
}
輸出結果:
Base Constructor
b1 constructed done
Base Copy Constructor
Base Destructor
Base Destructor
可見: 函數參數傳值調用也會調用拷貝構造函數
==================================================================
C. 測試派生類的拷貝構造函數
int main(int argc, char *argv[])
{
? ? Derived b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Derived b2 = b1;
? ? cout << "b2 constructed done" << ?std::endl;
? ? Derived b3(b2);
? ? cout << "b3 constructed done" << ?std::endl;
? ? return 1;
}
輸出結果:
Base Constructor
Derived Constructor
b1 constructed done
Base Constructor
Derived Copy Constructor
b2 constructed done
Base Constructor
Derived Copy Constructor
b3 constructed done
Derived Destructor
Base Destructor
Derived Destructor
Base Destructor
Derived Destructor
Base Destructor
結論:
1. 派生類的拷貝構造函數被調用前,會調用子類的構造函數;
總結
以上是生活随笔為你收集整理的C++派生类与基类构造函数调用次序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 商业用电电费多少钱一度?
- 下一篇: 《春雪》第十三句是什么