Nested Classes in C++
Home ] Up ] [ Nested Classes in C++ ] A Stack Class ] Inner Class Types ] Benefits of Inner Classes ]

 

 

C++ does not have inner classes, per se.  It has only nested classes

What's the difference?  C++ nested classes is a relationship between classes, as opposed to between objects.  As we'll see, Java inner classes usually do have a relationship between objects.

Here's an example of C++ nested classes. (Note that the implementation isn't completely spelled out, because we don't want to get bogged down in details that are not relevant to the topic at hand.)

#ifndef __LIST_H
#define __LIST_H
//
// LinkedList.h
//
#include <string>
using namespace std;
/////////// Class LinkedList ///////////////
class LinkedList
{
    class Link;     // Forward reference
public:
    LinkedList();   // Constructor
    ~LinkedList();  // Destructor
    // Access
    Link *GetFirst() const; // Returns ptr to first item in list
    Link *GetLast() const;  // Returns ptr to last item in list
    int GetCount() const;   // Returns count of items in list
    // Operations
    void Append(Link &item); // Appends an item to the end of the list
    void Purge();            // Removes all items from the list
    ////////// A public nested Iterator class ////////
    class Iterator
    {
    public:
        Iterator(const LinkedList &list);   // Constructor
        ~Iterator();                        // Destructor
        // Access
        const LinkedList *GetList() const; // Return ptr to the iterator's list
        Link *GetCurrent() const;    // Return ptr to current list item
        Link *GetNext();             // Move to next item and return ptr to it
        Link *GetPrevious();         // Move to previous item and return ptr to it
        bool  AtFirst() const;       // return true if current item is first item
        bool  AtLast() const;        // return true if current item is last item
    private:
        // Disable copy constructor for Iterator
        Iterator(const Iterator &);
        // Disable assignment for Iterator
        Iterator &operator=(const Iterator &);
        // Data
        const LinkedList *m_list;    // The list we are iterating through
        Link   *m_current;           // Points to the current item in the list
    };
private:
    // Disable copy constructor for LinkedList
    LinkedList(const LinkedList &);
    // Disable assignment for LinkedList
    LinkedList &operator=(const LinkedList &);
    // Data members
    Link *m_first;    // Pointer to the first list item
    Link *m_last;    // Pointer to the last list item
    int m_count;         // Count of list items currently in list
    ////////// A private nested Link class //////////////
    class Link
    {
    public:
        Link(void *data);    // Constructor (associates with data)
        ~Link();            // Destructor
        // Operations
        void Add(LinkedList &list, Link *prevItem);
        // Add this Link to the specified list
        // following the specified prevItem
        // (if prevItem is NULL, place at front of list)
        void Remove();            // Remove this list item from the list it is in
        // Access member functions
        Link *GetNext() const;     // Return ptr to next item in list
        Link *GetPrevious() const; // Return ptr to previous item in list
        const LinkedList *GetList() const;   // Return ptr to the list item's list
        void *GetData();               // Returns ptr to data for item
    private:
        // Disable copy constructor for Link
        Link(const Link &);
        // Disable assignment for Link
        Link &operator=(const Link &);
        // Data members
        Link       *m_next;    // Pointer to next item in the list
        Link       *m_prev;    // Pointer to previous item in the list
        LinkedList *m_list;    // Pointer to the list the item currently is in
        void       *m_data;    // Pointer to data
    };
};
#endif // __LIST_H

The basic benefits of nested classes in C++ are:

  • Access control -- you can place a nested class in a private, public, or protected section of the enclosing class
  • Naming -- if the nested class is to be used from outside of the enclosing class, its name must be qualified by the enclosing class's name.  So, for example, if you wanted to use the above nested Iterator class from outside the LinkedList class, you would have to refer to it as follows:
    LinkedList::Iterator

    That is, you would have to explicitly qualify it with the enclosing class's name.

 

This page was last modified on 02 October, 2007