C++派生类与基类构造函数调用次序
生活随笔
收集整理的這篇文章主要介紹了
C++派生类与基类构造函数调用次序
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本文用來(lái)測(cè)試C++基類和派生類構(gòu)造函數(shù),析構(gòu)函數(shù),和拷貝構(gòu)造函數(shù)的調(diào)用次序。
運(yùn)行環(huán)境: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. 測(cè)試派生類對(duì)象
int main(int argc, char *argv[])
{
? ? Derived d1;
? ? return 1;
}
輸出:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
結(jié)論:?
1.先構(gòu)造父類,才構(gòu)造子類,沒(méi)有父親,哪有兒子啊!
2.即使子類(派生類)沒(méi)有顯式(explicit)的調(diào)用(在初始化列表中調(diào)用)父類(基類)的構(gòu)造函數(shù),父類的構(gòu)造函數(shù)也會(huì)被調(diào)用;
================================================================
B. 測(cè)試基類的拷貝構(gòu)造函數(shù)
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;
}
輸出結(jié)果:
Base Constructor
b1 constructed done
Base Copy Constructor
b2 constructed done
Base Copy Constructor
b3 constructed done
Base Destructor
Base Destructor
Base Destructor
結(jié)論:
1. 代碼: "Base b2 = b1;" 和 "Base b3(b2);"
將調(diào)用拷貝構(gòu)造函數(shù),而不是其它的(賦值、構(gòu)造)函數(shù);
擴(kuò)展: 函數(shù)參數(shù)傳值調(diào)用也會(huì)調(diào)用拷貝構(gòu)造函數(shù)
添加函數(shù):
void Func(Base b)
{
}
int main(int argc, char *argv[])
{
? ? Base b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Func(b1);
? ? return 1;
}
輸出結(jié)果:
Base Constructor
b1 constructed done
Base Copy Constructor
Base Destructor
Base Destructor
可見(jiàn): 函數(shù)參數(shù)傳值調(diào)用也會(huì)調(diào)用拷貝構(gòu)造函數(shù)
==================================================================
C. 測(cè)試派生類的拷貝構(gòu)造函數(shù)
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;
}
輸出結(jié)果:
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
結(jié)論:
1. 派生類的拷貝構(gòu)造函數(shù)被調(diào)用前,會(huì)調(diào)用子類的構(gòu)造函數(shù);
運(yùn)行環(huán)境: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. 測(cè)試派生類對(duì)象
int main(int argc, char *argv[])
{
? ? Derived d1;
? ? return 1;
}
輸出:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
結(jié)論:?
1.先構(gòu)造父類,才構(gòu)造子類,沒(méi)有父親,哪有兒子啊!
2.即使子類(派生類)沒(méi)有顯式(explicit)的調(diào)用(在初始化列表中調(diào)用)父類(基類)的構(gòu)造函數(shù),父類的構(gòu)造函數(shù)也會(huì)被調(diào)用;
================================================================
B. 測(cè)試基類的拷貝構(gòu)造函數(shù)
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;
}
輸出結(jié)果:
Base Constructor
b1 constructed done
Base Copy Constructor
b2 constructed done
Base Copy Constructor
b3 constructed done
Base Destructor
Base Destructor
Base Destructor
結(jié)論:
1. 代碼: "Base b2 = b1;" 和 "Base b3(b2);"
將調(diào)用拷貝構(gòu)造函數(shù),而不是其它的(賦值、構(gòu)造)函數(shù);
擴(kuò)展: 函數(shù)參數(shù)傳值調(diào)用也會(huì)調(diào)用拷貝構(gòu)造函數(shù)
添加函數(shù):
void Func(Base b)
{
}
int main(int argc, char *argv[])
{
? ? Base b1;
? ? cout << "b1 constructed done" << ?std::endl;
? ? Func(b1);
? ? return 1;
}
輸出結(jié)果:
Base Constructor
b1 constructed done
Base Copy Constructor
Base Destructor
Base Destructor
可見(jiàn): 函數(shù)參數(shù)傳值調(diào)用也會(huì)調(diào)用拷貝構(gòu)造函數(shù)
==================================================================
C. 測(cè)試派生類的拷貝構(gòu)造函數(shù)
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;
}
輸出結(jié)果:
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
結(jié)論:
1. 派生類的拷貝構(gòu)造函數(shù)被調(diào)用前,會(huì)調(diào)用子類的構(gòu)造函數(shù);
總結(jié)
以上是生活随笔為你收集整理的C++派生类与基类构造函数调用次序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 商业用电电费多少钱一度?
- 下一篇: 《春雪》第十三句是什么