记录C++中,子类同名属性并不能完全覆盖父类属性的问题

问题代码:

首先看一段代码:很简单,就是BBB继承自AAA,然后BBB重写定义了同名属性,然后调用父类AAA的打印函数:

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {}
	~AAA() {}
	
	void func() {
		YourVar = 20;
	}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
public:
	int YourVar = 10;
	
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = 20;
	}
	~BBB() {}
	
	void print1() {
		cout << "YourVar: " << YourVar << endl;
	}
	
	void YourMethodOrFunction() {}
	
private:
	int YourVar;
};

int main() {
	BBB b;
	b.print();
	//b.print1();
	
	cout << "********" << endl;
	
	return 0;
}

打印结果:

会发现,明明我构造函数里面将YourVar的值改为了20,但是还是打印的10.

这个问题,属于C++的基础问题,我记得,子类继承父类,如果子类中,定义了同名属性的话,那么父类的属性会被隐藏,访问子类的那个属性,就是子类的,不会是父类的。

但是如果父类里面,有一个打印的函数print,会发现,这个print函数,生效的范围,只有父类,不包括子类。只有将父类的print函数重写,才能生效在子类中:

解决思路1:重写print

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {}
	~AAA() {}
	
	void func() {
		YourVar = 20;
	}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
public:
	int YourVar = 10;
	
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = 20;
	}
	~BBB() {}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
	void YourMethodOrFunction() {}
	
private:
	int YourVar;
};

int main() {
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

打印结果:

虽然解决了问题,但是明明我父类中,写了print函数,干嘛还重新写一次?还有意义?

解决思路2:去掉子类的同名属性

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {}
	~AAA() {}
	
	void func() {
		YourVar = 20;
	}
	
	void print() {
		cout << "YourVar: " << YourVar << endl;
	}
	
public:
	int YourVar = 10;
	
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = 20;
	}
	~BBB() {}
	
	
//private:
//	int YourVar;
};

int main() {
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

但是有时候,我们就是要这么重写,那么该怎么办呢?

解决思路3:将父类地址传递给子类

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {
		YourVar = new int;
		*YourVar = 10;
	}
	~AAA() {
		delete YourVar;
	}
	
	void print() {
		cout << "YourVar: " << *YourVar << endl;
	}
	
protected:
	int *YourVar;
};

class BBB : public AAA {
public:
	BBB() {

		// 将父类地址传递给子类
		YourVar = AAA::YourVar;
		*YourVar = 20;
	}
	~BBB() {}
	
private:
	int *YourVar;
};

int main() {
	
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

解决思路4:将子类地址传递给父类:

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {
		// YourVar = new int;
		// *YourVar = 10;
	}
	~AAA() {
		delete YourVar;
	}
	
	void print() {
		cout << "YourVar: " << *YourVar << endl;
	}
	
protected:
	int *YourVar;
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = new int;
		// 将子类地址传递给父类
		AAA::YourVar = YourVar;
		*YourVar = 20;
	}
	~BBB() {}
	
private:
	int *YourVar;
};

int main() {
	
	BBB b;
	b.print();
	
	cout << "********" << endl;
	
	return 0;
}

总结

  1. 面对这样的情况,要么就不要写同名属性(但有时候不能保证自己忘记了)

  2. 当以后继承的类层次很多的时候,难免会定义同名属性,但是父类的函数,是不会访问子类的同名属性的,这个是这个问题的关键。虽然在子类中,父类的属性确实是隐藏了,但隐藏不代表消失,也不代表父类的函数中,也将这个属性给隐藏或者替换了。

  3. 要么就使用指针,将父类和子类的地址变成一样的地址。

  4. 虽然最后一种比较麻烦,但是会惊奇的发现,如果是指针的话,可以实现父类中实现所有操作,子类只管创建对象和使用,子类如果想改实现,根据需要同名替代即可。甚至属性也可以写成函数指针那些,是一种非常灵活的使用方式,是一种上层设计模式的简单体现。

cpp 复制代码
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() {
		// YourVar = new int;
		// *YourVar = 10;
	}
	~AAA() {
		delete YourVar;
	}
	
	void print() {
		cout << "YourVar: " << *YourVar << endl;
	}
	
protected:
	int *YourVar;
};

class BBB : public AAA {
public:
	BBB() {
		YourVar = new int;
		// 将子类地址传递给父类
		AAA::YourVar = YourVar;
		*YourVar = 20;
	}
	~BBB() {}
	
	void print() {
		cout << "*********************" << endl;
		cout << "YourVar: " << *YourVar << endl;
		cout << "*********************" << endl;
	}
	
private:
	int *YourVar;
};

int main() {
	
	BBB b;
	b.print();
	
	return 0;
}
相关推荐
weixin_3077791321 分钟前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
开发者工具分享1 小时前
如何应对敏捷转型中的团队阻力
开发语言
gregmankiw1 小时前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
roman_日积跬步-终至千里1 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
秦少游在淮海2 小时前
C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
开发语言·c++·stl·string·范围for·auto·string 的使用
const5442 小时前
cpp自学 day2(—>运算符)
开发语言·c++
心扬2 小时前
python生成器
开发语言·python
阿蒙Amon2 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
虾球xz2 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
CodeWithMe2 小时前
【C/C++】namespace + macro混用场景
c语言·开发语言·c++