类和类之间访问私有变量

cpp 复制代码
#include<iostream>
using namespace std;
class Building;
class Gooddey{

public:
	friend  Building;
	Gooddey();
	void visit();
	Building *building;
};
class Building{
	friend	class Gooddey;
public:
	Building(){
		this->age = 10;
		this->ac = 20;
	}
public:
	int age ;
private:
	int ac ;
};
Gooddey::Gooddey(){
	building = new Building;
}
void Gooddey::visit(){
	cout << building->age << endl;
	cout << building->ac << endl;
}
void test(){
	Gooddey p;
	p.visit();
}
int main(){
	test();
}
相关推荐
纵有疾風起18 小时前
C++——多态
开发语言·c++·经验分享·面试·开源
稚辉君.MCA_P8_Java18 小时前
Gemini永久会员 Java实现的暴力递归版本
java·数据结构·算法
冯诺依曼的锦鲤18 小时前
算法练习:差分
c++·学习·算法
有意义19 小时前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
鹿鹿鹿鹿isNotDefined19 小时前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
Mr_WangAndy19 小时前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
氵文大师19 小时前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
封奚泽优19 小时前
下降算法(Python实现)
开发语言·python·算法
im_AMBER19 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
高洁0119 小时前
【无标具身智能-多任务与元学习】
神经网络·算法·aigc·transformer·知识图谱