DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Nested Class Definitions

Hybrid View

  1. #1
    Join Date
    Dec 2003
    Posts
    38

    Nested Class Definitions

    In most C++ textbooks class definitions are very simplistic, ie., normally you have private: followed by a few identifiers and then public: followed by a few member functions. Is it a good idea to use class or even define class definitions within another class definition with the possibility of having member functions defined within. Or is this bad practice?

    EVAC

  2. #2
    Join Date
    Jul 2005
    Posts
    26
    I do not know whether it is a good practice, but from what I can tell, I never actually used it.

    Sometimes when I needed a struct that is solely used in one class, I made it a member of that class. Most of the times, a littel later on, I decided that it would be better if the class members were automatically initialized, destroyed, need an member variable access function or whatever, so I made a class of it. Once done, I feel queasy with the nested syntax and moved the nested class out of the outer class definition.

    Another time I realized that I need the class to be returned by a member function of the outer class and feel bad about requiring my clients to create or handle a nested class.

    OTOH it is a very common practice in Java - although I mostly dislike the syntax.

    Well, maybe there are situations when nested classes are handy or stylish and someone can give an example.

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Actually, nested classes are quite useful in certain cases. The Pimpl idiom for example requires that each class define a nested Pimpl struct:
    http://www.devx.com/cplus/Article/28105


    Even if you don't use this idiom, a nested class can solve a few encapsulation issues rather neatly. The main criterion for using a nested class is: "am I sure that no other client/class should use this nested class?" If the answer is positive, then a nested class is probably a good design choice. Notice that if you have public member functions in the enclosing class that return the nested class or expose its existence in any other way, then nesting was probably a bad idea to begin with.
    Danny Kalev

  4. #4
    Join Date
    Jul 2005
    Posts
    26
    Quote Originally Posted by Danny
    Actually, nested classes are quite useful in certain cases. The Pimpl idiom for example requires that each class define a nested Pimpl struct
    You are right that is is convenient for the pimpl idiom to use a nested class, although it is not required. I'd personally consider this as an exception.

    Quote Originally Posted by Danny
    Even if you don't use this idiom, a nested class can solve a few encapsulation issues rather neatly.
    What do you particularly think about?

    Quote Originally Posted by Danny
    The main criterion for using a nested class is: "am I sure that no other client/class should use this nested class?" If the answer is positive, then a nested class is probably a good design choice.
    That is exactly my point: I sometimes thought it were a good design choice until new project requirements lead me to a point where it would be best to dispose the nested class to the clients. I could have saved up the required modifications if I just had not nested the class. Apart from a more beautiful looking design, I do not see a clue in nesting a classes.

  5. #5
    Join Date
    Dec 2003
    Posts
    38
    Hey thanks guys for the assistance. Much appreciated. Both responses were definitely on point. I especially like the "I feel queasy" sentiment. :p

    EVAC

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by Oliver
    You are right that is is convenient for the pimpl idiom to use a nested class, although it is not required. I'd personally consider this as an exception.


    What do you particularly think about?
    In cases when a class needs a helper class that's reposnsible for some internal bookkeeping, and you want to hide that helper class from clients, a nested class can do the trick. Take for example a string class and a helper class that's reposinsible for determining its collating order. The collating order class isn't meant to be used by other classes or algorithms, or it might be changed frequently according to customers' demands, so you can implement it as a nested class. Another example: a function object implemented inside a class.
    If you're looking for a real world example, here's one:
    http://www.dinkumware.com/htm_cpl/ve...ctor%3Cbool%3E
    The type vector<bool>::reference is actually a nested class. It makes much sense to use a nested class in this case because other STL containers define reference as a fundamental datatype (i.e., plain references).
    Last edited by Danny; 07-29-2005 at 09:11 PM.
    Danny Kalev

  7. #7
    Join Date
    Jul 2005
    Posts
    26
    Yes, Danny, you're absolutely right about helper classes as pointed out in your example. Moreover, what I didn't consider in my previous posts, I sometimes use nested function object classes.

    Maybe I was way too occupied by my design mistakes (when I needed to "unnest" a class later), so I did not consider this usage.

    Well, what we maybe might drew out of this discussion is, that nested class designs should be handled with care and foresight but can be really useful to encapsulate class usage and prevent namespace pollution.

    Would you agree with that?

Similar Threads

  1. Replies: 2
    Last Post: 05-24-2005, 06:56 AM
  2. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  3. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  4. Replies: 1
    Last Post: 10-20-2001, 05:27 PM
  5. Replies: 333
    Last Post: 06-19-2001, 09:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center