亲宝软件园·资讯

展开

C++继承

code-016 人气:0

一、构造/析构顺序及继承性

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

在这里插入图片描述

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!     

加载全部内容

相关教程
猜你喜欢
用户评论