6

本地类、内部类和嵌套类在 C++ 中的含义相同吗?

4

3 回答 3

7

Local Class and Nested class are different beasts.

A Nested class is a class declared within the scope of another class.

A Local class is declared within a function definition.

Inner class is a non standard C++ term, So I am not sure how to define it.


Nested Classes:

IBM Documentation as a nice documentation here.
To Summarize:

  • The name of a nested class is local to its enclosing class. Unless you use explicit pointers, references, or object names, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class and global variables.
  • Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes. Member functions of the enclosing class have no special access to members of a nested class

Local Classes:
This answer of mine here documents the subtle points associated with local classes.

于 2011-10-24T15:39:54.650 回答
2

引用 C++11 (N3290) 草案:

9.7 嵌套类声明[class.nest]

1 一个类可以在另一个类中声明。在另一个类中声明的类称为嵌套类。

9.8 本地类声明[class.local]

1 类可以在函数定义中声明;这样的类称为本地类。

C++ 标准中没有指定内部类的概念。

于 2011-10-24T16:00:48.767 回答
0

Inner classes capture the enclosing class's this reference. A hierarchy of inner class instances form a tree structure. This is unique for Java.

于 2011-10-24T17:41:03.000 回答