记录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;
}
相关推荐
xu_yule1 小时前
算法基础(数论)—费马小定理
c++·算法·裴蜀定理·欧拉定理·费马小定理·同余方程·扩展欧几里得定理
宇宙超级无敌暴龙战士5 小时前
旮旯c语言三个任务
c++·c
我居然是兔子5 小时前
异常练习:在试错中吃透Java异常处理的底层逻辑
java·开发语言
养一回月亮!5 小时前
使用Qt实现简单绘图板:鼠标绘制与擦除功能详解
开发语言·qt
BanyeBirth6 小时前
C++差分数组(二维)
开发语言·c++·算法
Tony Bai6 小时前
Go 的 AI 时代宣言:我们如何用“老”原则,解决“新”问题?
开发语言·人工智能·后端·golang
Fcy6486 小时前
C++ map和multimap的使用
开发语言·c++·stl
CC.GG6 小时前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
L Jiawen7 小时前
【Golang基础】基础知识(下)
服务器·开发语言·golang
lengjingzju7 小时前
基于IMake的 GCC 编译与链接选项深度解析:构建高效、安全、可调试的现代软件
c++·安全·性能优化·软件构建·开源软件