C++ - What's the point of nested classes?

前端 未结 3 1924
星月不相逢
星月不相逢 2021-02-01 15:04

I\'m studying a little of C++ and now I\'m fighting against it\'s similitudes with Java. I know the purpose of inner classes in Java, but now I\'m trying to use nested classes i

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 15:41

    Different isn't the same.

    Java's inner classes create objects that are assumed to be connected with an object of the outer class, so access to members of the outer class from methods of the inner class can be done without explicitly creating a pointer or reference. C++ doesn't do that; a nested class is just a class whose definition is nested inside the definition of another class. That's handy for encapsulation, but that's it: it's not intended to magically make objects of that type know about objects of the containing type.

提交回复
热议问题