Are Local class, Inner class and Nested class are the same things in C++?

3.1k views Asked by At

Are Local class, Inner class and Nested class mean same things in C++?

3

There are 3 answers

1
Alok Save On BEST ANSWER

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.

0
mloskot On

Quoting draft of C++11 (N3290):

9.7 Nested class declarations [class.nest]

1 A class can be declared within another class. A class declared within another is called a nested class.

9.8 Local class declarations [class.local]

1 A class can be declared within a function definition; such a class is called a local class.

There is no concept of inner class specified in C++ standard.

0
Jörgen Sigvardsson On

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