欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

C++ 类和对象 - 3. 内部类

最编程 2024-10-07 12:25:10
...

内部类默认时其外部的友元类

#include<iostream>
using namespace std;
class A
{
private:
	static int _k;
	int _h = 1;
public:
	class B // B默认就是A的友元
	{
	public:
		void foo(const A& a)
	{
		cout << _k << endl; //OK
		cout << a._h << endl; //OK
	}
	};
};
int A::_k = 1;
int main()
{
	cout << sizeof(A) << endl;
	A::B b;
	A aa;
	b.foo(aa);
	return 0;
}

这样就可以将前面的题目进行改进一下:

image-20241007105459373

推荐阅读