公共嵌套类和普通类有什么区别吗?
假设我有:
class A {
public:
class B {
};
};
公共嵌套类和在其自己的 cpp 文件中定义的常规 B 类之间有什么区别,除了 A::B 必须在第一个选项中使用这一事实之外?
Let's say I have:
class A {
public:
class B {
};
};
Is there any difference between that public nested class and just a regular B class which is defined in its own cpp file, except for the fact that A::B must be used in the first option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本质上没有区别,只是
A::B
是A
的成员,因此拥有A
私有成员的所有访问权限> 任何其他成员都会有的。There is essentially no difference, except that
A::B
is a member ofA
, and so has all the access rights to private members ofA
that any other member would have.除了“B”的范围规则之外没有任何区别。使用“B”的客户端必须使用“A::”限定其范围。当您想要转发引用“B”时,嵌套“B”有时可能会出现问题,因为 C++ 编译器通常不允许您转发引用类中的类(但它允许您转发引用命名空间中的类)。
There isn't any difference other than the scoping rules for "B". Clients that use "B" must qualify its scope with "A::". Nesting the "B" can sometimes be problematic when you want to forward reference it, since C++ compilers typically do not allow you to forward reference a class within a class (it does allow you to forward reference a class within a namespace though).