类和类之间访问私有变量

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();
}
相关推荐
王老师青少年编程3 小时前
2026年6月GESP真题及题解(C++一级):交税
c++·题解·真题·gesp·一级·2026年6月·交税
Jerry3 小时前
LeetCode 160. 相交链表
算法
认真的酒窝3 小时前
自己动手开发编译器(十一)语义分析
java·开发语言
Jerry4 小时前
LeetCode 19. 删除链表的倒数第 N 个结点
算法
金銀銅鐵4 小时前
费马小定理
python·数学·算法
wbs_scy5 小时前
Linux C++ 高并发编程:线程池全链路深度解析,从原理到手撕实现
java·开发语言
JAVA面经实录9175 小时前
Linux 常用命令完整知识体系
java·linux·开发语言·汇编
技术不好的崎鸣同学5 小时前
[ACTF2020 新生赛]Exec 思路及解法
算法·安全·web安全
贪玩的蛋挞5 小时前
C#与闭包
开发语言·c#
端庄的战斗机6 小时前
javascript 设计模式(文章很长,请自备瓜子,水果和眼药水)
开发语言·javascript·设计模式