让内部类成为 C++ 中的朋友
我想让一个内部类成为一个不相关类的友元,但这似乎不起作用(至少在 gcc 4.1.2 中):
class A {
int i;
friend class B; // fine
friend class B::C; // not allowed?
};
class B {
int geti(A* ap) { return ap->i; }
class C {
int geti(A* ap) { return ap->i; }
};
};
I want to make an inner class a friend of an unrelated class but this doesn't seem to work (at least in gcc 4.1.2):
class A {
int i;
friend class B; // fine
friend class B::C; // not allowed?
};
class B {
int geti(A* ap) { return ap->i; }
class C {
int geti(A* ap) { return ap->i; }
};
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用它之前,您必须声明
B::C
。以下可能有效。更新:忽略所要求的可用演示,这里有一种可以工作的构造方法(减去成员函数的定义),但请记住,一切都是私有的。
然后在别处定义 getter 函数:
替代方案:前向声明嵌套类
B::C
并保存一个外部定义:You have to declare
B::C
before using it. The following might work.Update: Ignoring a usable demonstration as requested, here's a way of structuring this (minus the definitions of member functions) that could work, but bear in mind that everything is private as it stands.
Then define the getter functions elsewhere:
Alternative: forward-declare the nested class
B::C
and save one external definition: