Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's an excerpt from "The C++ Programming Language"

template<classT> class List { // optimal
class Link { /* ... */ };
List (); // initially empty
void put(T *); // put before current element
T* get(); // get current element
/ / ...

Can we make a class inside another class ? How about other languages? For PHP see this question

share|improve this question
up vote 14 down vote accepted

It's called a nested class. Some languages (C++) allow it, others (PHP) don't.

It is not technically necessary for a language to support it, but sometimes, it helps prevent cluttering outer namespaces. In the case of STL, it makes for a neat way of organizing dependent types such as iterators (e.g. list<T>::iterator and vector<T>::iterator instead of list_iterator<T> and vector_iterator<T>, the former signalling functional dependency much more clearly).

share|improve this answer
@PéterTörök: Thanks, disambiguated ;-) – tdammers May 31 '12 at 8:11

Can we make a class inside another class ?

Yes, in C++ you can, it is called a nested class. However, this class is more like an extension of it's containing class, so don't forget that the sub class can access it's containing class members even if they are private.

See … also

How about other languages?

You'll have to check fo each language but I know at least Python allow this, with different semantic. Don't assume features are providing exactly the same semantic between languages.

share|improve this answer…: 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. – Septagram May 31 '12 at 6:06
@Septagram You are wrong:… also – Klaim May 31 '12 at 6:15
@Septagram Or maybe it have changed in the last standard? Can you point standard informations? – Klaim May 31 '12 at 6:17
Seems like I really am wrong. I'm sorry :( – Septagram May 31 '12 at 6:49
@Septagram No problem. I edited my question, you can upvote if you want to. – Klaim May 31 '12 at 7:08

Sure thing! Although it doesn't do anything except change the scope and perhaps give structure to your code.

Also worth noting is Java's inner class feature. It's a nested class that can access instance variables of the enclosing class. E.g. if you have a class, Outer, which contains an inner class, Inner, you can access Outer's fields from within the inner class with the syntax Outer.this.banana (given that Outer has the field banana). In order to create an instance of an inner class one first has to have an instance of the outer class. This also means that an object of the inner class cannot outlive the its outer class. I find this a very handy feature of Java. To get the behavior of a normal nested class in Java, you declare it static.

Other languages that I'm aware of supporting nested classes (not necessarily inner classes) are C++, C# and python.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.