Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 592
user366312 Avatar asked Oct 24 '11 15:10

user366312


People also ask

Is there any difference between an inner class and nested class?

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.

Is local class an inner 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.


2 Answers

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.

like image 199
Alok Save Avatar answered Oct 05 '22 23:10

Alok Save


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.

like image 33
mloskot Avatar answered Oct 05 '22 21:10

mloskot