2

Possible Duplicate:
Pros and cons of using nested C++ classes and enumerations?

Consider the following declaration.

class A {
    public:
    class B{
    };
};

Nothing special.

  • But what are the benefits of this?
  • What reasons may there be for putting one class inside of another?
  • There is no inheritance benefit between both classes.
  • If B is put inside of A for its private names sharing, then A is for B just a namespace, and there is reason to make B private, too.

What do you think about this?

Community
  • 1
  • 1
OlegG
  • 975
  • 3
  • 10
  • 30

3 Answers3

4

Conceptually, it lets the programmer(s) know that class B relates specifically to class A. When you use class B outside of class A, you must use the type as A::B, which reminds you, every time, that B is related to A. This doesn't add any functionality, but shows a relationship.

Similarly, you don't have to use inheritance/composition, or classes at all. You can more or less simulate classes in C just by using structs and functions. It's just a way to keep the code cleaner and more straightforward and let the programmer relate the code to the design more easily. Classes accomplish this much more than public subclasses do, but that's just an example.

If it's a private/protected subclass (which I see it isn't in your example), then that obviously limits that class to the implementation of that class and that class's children, which might be desired (again design-wise) if the only use case of that class is in the implementation of that class (and possibly its children).

Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
1

Benefit 1: The namespace aspect

Indeed, A provides a namespace for B, and this can help us structure our code much better. Consider a concrete example with vector for A, and iterator for B. Arguably,

class vector {
public:
  class iterator { /*...*/ };

  iterator begin() { /*...*/ }
};

is easier to type, to read, and to understand than

class vector_iterator {
  /*...*/
};

class vector {
public:
  vector_iterator begin() { /*...*/ }
};

Observe, in particular:

  1. When the two classes (vector and iterator) depend on each other, i.e. use each other's members, the second version above would require one of the two to be forward-declared, and in some cases mutual type-dependencies might lead to unresolvable situations. (Using nested classes, it is much easier to avoid such problems, because within most parts of the nested class definition, the outer class is considered completely-defined. This is due to §9.2/2.)

  2. You may very well have many other data types that maintain their own iterator, e.g. linked_list. Using the second version above, you'd need to define linked_list_iterator as a separate class. Class names would get ever longer and complicated the more of these 'dependent' types and alternative types you added.

Benefit 2: Templates

Continuing the example above, consider now a function template that takes a container (such as vector and linked_list defined above) as arguments and iterates over them:

template <typename Container>
void iterate(const Container &container) {
  /*...*/
}

Inside this function, you'd obviously very much like to use the iterator type of Container. If that is a nested type, it's easy:

typename Container::iterator

But if it isn't, you would have to take the iterator type as a separate template parameter:

template <typename Container, typename Iterator>
void iterate(const Container &container) {
  /*...*/
  Iterator it = container.begin();
  /*...*/
}

And if that iterator type does not appear among the function arguments, the compiler could not even guess the type. You'd have to explicitly add it in angle brackets each time you call the iterate function.

Final notes: None of this has much to do with whether the nested class is declared as public or private. My examples above suggest a situation in which a public nested type is clearly preferrable, because I suppose the iterator type should be able to be used outside the container class.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
0

What reasons may be for putting one class inside of another one?

  • If you need to restrict the scope of B to only available for A, then internal class definition helps. Because it restricts the class scope to local.This is call scope localization.These are in more generic term called inner class declaration. Check this link.
  • This stackoverflow question helps you understand more.
Community
  • 1
  • 1
kumar_m_kiran
  • 3,982
  • 4
  • 47
  • 72
  • 1
    In this case B is available for any who wants to use it. Because it is public member of A. – OlegG Nov 06 '12 at 07:56
  • But, if B is outside A, then it cannot access private members of A (unless you declare as friends). Check this link - http://stackoverflow.com/questions/3791296/is-there-any-difference-between-a-public-nested-class-and-a-regular-class – kumar_m_kiran Nov 06 '12 at 11:55