类和类之间访问私有变量

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();
}
相关推荐
惯导马工32 分钟前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农2 小时前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了2 小时前
AcWing学习——双指针算法
c++·算法
感哥2 小时前
C++ 指针和引用
c++
moonlifesudo3 小时前
322:零钱兑换(三种方法)
算法
感哥13 小时前
C++ 多态
c++
沐怡旸20 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
NAGNIP20 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队21 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
River4161 天前
Javer 学 c++(十三):引用篇
c++·后端