Is there any difference between a public nested class and a regular class?

那年仲夏 提交于 2019-12-14 02:15:15

问题


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?


回答1:


There is essentially no difference, except that A::B is a member of A, and so has all the access rights to private members of A that any other member would have.




回答2:


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).



来源:https://stackoverflow.com/questions/3791296/is-there-any-difference-between-a-public-nested-class-and-a-regular-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!