繼承的時候,子類的拷貝建構函式和過載運算子的實現
由[1] [2] 例子看出:子類中實現了拷貝構造和賦值函式,則呼叫子類的拷貝構造和賦值函式
[1]說明:
子類的拷貝構造中呼叫父類的拷貝構造;
子類的賦值函式中呼叫父類的賦值函式
#include<iostream> using namespace std; class Parent { public: Parent(){cout<<"Parent constru fun()"<<endl;} Parent(int a):m_a(a){cout<<"Parent constru fun(int )"<<endl;} Parent(const Parent&other):m_a(other.m_a) { cout<<"Parent copy fun"<<endl; } Parent& operator= (const Parent& other) { cout<<"Parent =fun"<<endl; if(this == &other) return *this; m_a = other.m_a; return *this; } int geta() { return m_a; } private: int m_a; }; class Child : public Parent { public: Child(){cout<<"Child constru fun( )"<<endl;} Child(int a, int b):Parent(a),m_b(b){cout<<"Child constru fun(int, int)"<<endl;} Child(const Child& other) :Parent(other) //呼叫父類的拷貝建構函式 { // Parent::Parent(other); //呼叫父類的拷貝建構函式(同上面的初始化列表) m_b = other.m_b; cout<<"Child copyfun"<<endl; } Child& operator= (const Child&other) { cout<<"Child =fun"<<endl; Parent::operator=(other); //呼叫父類的過載賦值運算子 if(this == &other) return *this; m_b = other.m_b; return *this; } int getb() { return m_b; } private: int m_b; }; int main() { Child child1(2, 3); cout<<"========================================="<<endl; Child child2(child1);/*呼叫子類的拷貝構造,而子類的拷貝構造會先呼叫父類的拷貝建構函式*/ cout<<"a ="<<child2.geta()<<",b ="<<child2.getb()<<endl; cout<<"========================================="<<endl; child2 = child1; /*呼叫子類的=函式,而子類的=函式會先呼叫父類的=函式*/ cout<<"a ="<<child2.geta()<<",b ="<<child2.getb()<<endl; return 0; }
輸出結果:
[email protected]:/home/ygm# ./mai
Parent constru fun(int )
Child constru fun(int, int)
=========================================
Parent copy fun
Child copyfun
a =2,b =3
=========================================
Child =fun
Parent =fun
a =2,b =3
[2]說明:
子類中拷貝建構函式未呼叫父類的拷貝建構函式;
子類的賦值號函式中未呼叫父類的賦值號函式;
#include <iostream> using namespace std; class Parent { public: Parent(){cout<<"Parent constru ()"<<endl;} Parent(int a):m_a(a){cout<<"Parent constru (int)"<<endl;} Parent(const Parent& other):m_a(other.m_a) { cout<<"Parent copy constru"<<endl; } Parent& operator= (const Parent& other) { cout<<"Parent = fun"<<endl; if(this == &other) return *this; m_a = other.m_a; return *this; } private: int m_a; }; class Child : public Parent { public: Child(){cout<<"Child constru ()"<<endl;} Child(int a, int b):Parent(a), m_b(b){cout<<"Child constru (int, int)"<<endl;} //未呼叫父類的拷貝構造 Child(const Child& other) { m_b = other.m_b; cout<<"Child copy constru"<<endl; } //未呼叫父類的賦值函式 Child& operator= (const Child& other) { cout<<"Child = fun"<<endl; if(this == &other) return *this; m_b = other.m_b; return *this; } private: int m_b; }; int main() { Child child1(2, 3); cout<<"==================="<<endl; Child child2(child1); cout<<"==================="<<endl; child2 = child1; return 0; }
輸出:
[email protected]:/home/ygm# ./mai
Parent constru (int)
Child constru (int, int)
===================
Parent constru ()
Child copy constru
===================
Child = fun
[3]說明:
子類中沒有實現拷貝構造和賦值函式:則採取預設的流程,
首先呼叫父類的拷貝建構函式。然後呼叫子類系統的拷貝建構函式。
首先呼叫父類的賦值過載函式。然後呼叫子類系統的賦值過載函式。
#include <iostream>
using namespace std;
class Parent
{
public:
Parent(){cout<<"Parent constru ()"<<endl;}
Parent(int a):m_a(a){cout<<"Parent constru (int)"<<endl;}
Parent(const Parent& other):m_a(other.m_a)
{
cout<<"Parent copy constru"<<endl;
}
Parent& operator= (const Parent& other)
{
cout<<"Parent = fun"<<endl;
if(this == &other)
return *this;
m_a = other.m_a;
return *this;
}
private:
int m_a;
};
class Child : public Parent
{
public:
Child(){cout<<"Child constru ()"<<endl;}
Child(int a, int b):Parent(a), m_b(b){cout<<"Child constru (int, int)"<<endl;}
private:
int m_b;
};
int main()
{
Child child1(2, 3);
cout<<"==================="<<endl;
Child child2(child1);
cout<<"==================="<<endl;
child2 = child1;
return 0;
}
輸出:
[email protected]:/home/ygm# ./mai
Parent constru (int)
Child constru (int, int)
===================
Parent copy constru
===================
Parent = fun
陽光夢總結:
[1]子類中沒有實現拷貝構造和賦值函式:
預設的拷貝建構函式的行為:
首先呼叫父類的拷貝建構函式。然後為自己獨有的各成員尋找拷貝建構函式。
如果這個類提供重寫的拷貝建構函式,則呼叫重寫的拷貝建構函式,如果成員的類提供的拷貝建構函式是private,編譯將無法通過。;
如果這個類沒有重寫拷貝建構函式,則呼叫預設的拷貝建構函式;
預設的賦值運算的行為:
首先呼叫父類的賦值過載函式。然後為自己獨有的各成員尋找賦值過載函式
。
如果這個類提供重寫的賦值過載函式,則呼叫重寫的
賦值過載函式
,如果成員的類提供的
賦值過載函式
是private,編譯將無法通過。;
如果這個類沒有重寫賦值過載函式,則呼叫預設的賦值過載函式;
[2]子類中實現了拷貝構造和賦值函式:
則呼叫子類的拷貝構造和賦值函式