Are Local class, Inner class and Nested class mean same things in C++?
A class that is defined within another class is called a nested class. An inner class, on the other hand, is a non-static type, a particular specimen of a nested class.
Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With