欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載詳解

 更新時(shí)間:2022年03月04日 17:09:09   作者:code-016  
這篇文章主要為大家詳細(xì)介紹了C++繼承中的對(duì)象構(gòu)造與析構(gòu)和賦值重載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

一、構(gòu)造/析構(gòu)順序及繼承性

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;
	}
};

在這里插入圖片描述

結(jié)論:

1.構(gòu)造順序:先構(gòu)造基類(lèi),后構(gòu)造派生類(lèi)

2.析構(gòu)順序:先析構(gòu)派生類(lèi),后析構(gòu)基類(lèi)

二、拷貝構(gòu)造的繼承性

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;
	}
};

在這里插入圖片描述

結(jié)論:

1.先調(diào)用基類(lèi)缺省的構(gòu)造函數(shù),后調(diào)用派生類(lèi)的拷貝構(gòu)造函數(shù)

2.若派生類(lèi)沒(méi)有缺省構(gòu)造函數(shù)A(),就會(huì)報(bào)錯(cuò)

疑惑:如何去調(diào)用基類(lèi)的拷貝構(gòu)造而不是缺省構(gòu)造

#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)	//發(fā)生賦值兼容規(guī)則(切片)
	{
		cout << "B(const B& src)" << this << endl;
	}
	~B()
	{
		cout << "~B()" << this << endl;
	}
};
int main()
{
	B b(10);
	B b1(b);
	return 0;
}

在這里插入圖片描述

結(jié)果:

將B類(lèi)型src傳遞給A類(lèi)型的A(const A& src)拷貝構(gòu)造函數(shù),發(fā)生了賦值兼容規(guī)則(切片現(xiàn)象)

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

#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)	//發(fā)生賦值兼容規(guī)則(切片)
	{
		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;
}

在這里插入圖片描述

結(jié)論:默認(rèn)情況下僅僅調(diào)用了派生類(lèi)的對(duì)象的賦值重載,并未調(diào)用基類(lèi)的賦值重載。

解決方案:

#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)	//發(fā)生賦值兼容規(guī)則(切片)
	{
		cout << "B(const B& src)" << this << endl;
	}
	B& operator=(const B& src)
	{
		if(this != &src)
		{
			*(A*)this = src;	//將調(diào)用基類(lèi)賦值重載
			_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;
}

在這里插入圖片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!     

相關(guān)文章

最新評(píng)論