首頁 > 軟體

C++繼承中的物件構造與解構和賦值過載詳解

2022-03-04 19:00:10

一、構造/解構順序及繼承性

class A
{
private:
	int _a;
public:
	A(int a = 0): _a(a)
	{
		cout << "A()" << this << endl;
	}
	~A()
	{
		cout << "~A()"<< this <<endl;
	}
};

class B : public A
{
private:
	int _b;
public:
	B(int b): _b(b), A()
	{
		cout << "B()" << this << endl;
	}
	~B()
	{
		cout << "~B()"<< this <<endl;
	}
};

結論:

1.構造順序:先構造基礎類別,後構造派生類

2.解構順序:先解構派生類,後解構基礎類別

二、拷貝構造的繼承性

class A
{
private:
	int _a;
public:
	A(int a = 0): _a(a)
	{
		cout << "A()" << this << endl;
	}
	A(const A& src): _a(src._a)
	{
		cout << "A(const A& src)"<< this << endl;
	}
	~A()
	{
		cout << "~A()"<< this <<endl;
	}
};

class B : public A
{
private:
	int _b;
public:
	B(int b): _b(b), A()
	{
		cout << "B()" << this << endl;
	}
	B(const B& src): _b(src._b)
	{
		cout << "B(const B& src)" << this << endl;
	}
	~B()
	{
		cout << "~B()"<< this <<endl;
	}
};

結論:

1.先呼叫基礎類別預設的建構函式,後呼叫派生類的拷貝建構函式

2.若派生類沒有預設建構函式A(),就會報錯

疑惑:如何去呼叫基礎類別的拷貝構造而不是預設構造

#include<iostream>
using namespace std;

class A
{
private:
	int _a;
public:
	A(int a = 0) : _a(a)
	{
		cout << "A()" << this << endl;
	}
	A(const A& src) : _a(src._a)
	{
		cout << "A(const A& src)" << this << endl;
	}
	~A()
	{
		cout << "~A()" << this << endl;
	}
};

class B : public A
{
private:
	int _b;
public:
	B(int b) : _b(b), A()
	{
		cout << "B()" << this << endl;
	}
	B(const B& src) : _b(src._b), A(src)	//發生賦值相容規則(切片)
	{
		cout << "B(const B& src)" << this << endl;
	}
	~B()
	{
		cout << "~B()" << this << endl;
	}
};
int main()
{
	B b(10);
	B b1(b);
	return 0;
}

結果:

將B型別src傳遞給A型別的A(const A& src)拷貝建構函式,發生了賦值相容規則(切片現象)

三、賦值過載不具有繼承性

#include<iostream>
using namespace std;

class A
{
private:
	int _a;
public:
	A(int a = 0) : _a(a)
	{
		cout << "A()" << this << endl;
	}
	A(const A& src) : _a(src._a)
	{
		cout << "A(const A& src)" << this << endl;
	}
	A& operator=(const A& src)
	{
		if(this != &src)
		{
			_a = src._a;
			cout << "A& operator=(const A& src)" << endl;
		}
	}
	~A()
	{
		cout << "~A()" << this << endl;
	}
};

class B : public A
{
private:
	int _b;
public:
	B(int b) : _b(b), A()
	{
		cout << "B()" << this << endl;
	}
	B(const B& src) : _b(src._b), A(src)	//發生賦值相容規則(切片)
	{
		cout << "B(const B& src)" << this << endl;
	}
	B& operator=(const B& src)
	{
		if(this != &src)
		{
			_b = src._b;
			cout << "B& operator=(const B& src)" <<  endl;
		}
	}
	~B()
	{
		cout << "~B()" << this << endl;
	}
};
int main()
{
	B b1(10);
	B b2(20);
	b1 = b2;
	return 0;
}

結論:預設情況下僅僅呼叫了派生類的物件的賦值過載,並未呼叫基礎類別的賦值過載。

解決方案:

#include<iostream>
using namespace std;

class A
{
private:
	int _a;
public:
	A(int a = 0) : _a(a)
	{
		cout << "A()" << this << endl;
	}
	A(const A& src) : _a(src._a)
	{
		cout << "A(const A& src)" << this << endl;
	}
	A& operator=(const A& src)
	{
		if(this != &src)
		{
			_a = src._a;
			cout << "A& operator=(const A& src)" << endl;
		}
	}
	~A()
	{
		cout << "~A()" << this << endl;
	}
};

class B : public A
{
private:
	int _b;
public:
	B(int b) : _b(b), A()
	{
		cout << "B()" << this << endl;
	}
	B(const B& src) : _b(src._b), A(src)	//發生賦值相容規則(切片)
	{
		cout << "B(const B& src)" << this << endl;
	}
	B& operator=(const B& src)
	{
		if(this != &src)
		{
			*(A*)this = src;	//將呼叫基礎類別賦值過載
			_b = src._b;
			cout << "B& operator=(const B& src)" <<  endl;
		}
	}
	~B()
	{
		cout << "~B()" << this << endl;
	}
};
int main()
{
	B b1(10);
	B b2(20);
	b1 = b2;
	return 0;
}

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!     


IT145.com E-mail:sddin#qq.com