Thread: Nested class

  1. #1
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91

    Nested class

    Hi everyone,

    Anyone can tell in which situations nested class becomes the best solution.

    Of course we learnt it from textbook, but so far in real practices, I never used, infact avoid it. At some point, it makes the code complex and difficult to debug.

    Anyone with me?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    100
    I haven't heard of nested classes being available in C++, and my understanding until now is that they aren't; but even if they are, it's a very weird thing to do in that language and something that you probably generally just want to avoid.

  3. #3
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    The first example that comes to my mind is iterators in the STL.

    To my knowledge, the containers in the STL implement their own versions of all iterators privately (probably as nested classes), then typedef them to just be "iterator" publicly.

    It's about encapsulation. When you want an object that is similar between classes, but specific to a class.

    For example, we are both people. For all intensive purposes, we are probably pretty much the same.

    But let's pretend that we are citizens of different countries, and that because of this, we greet people differently.

    Consider this sample code (or see it run here):

    Code:
    #include <iostream>
    
    class CountryA
    {
      class CountryA_Citizen; // forward declaration
    
    public:
      typedef CountryA_Citizen citizen;
      static citizen new_citizen() { return citizen(); }
      
    private:
      class CountryA_Citizen
      {
      public:
        CountryA_Citizen() {}
        std::string greeting() { return "Hey! How have you been?"; }
      };
    };
    
    class CountryB
    {
      class CountryB_Citizen; // forward declaration
    
    public:
      typedef CountryB_Citizen citizen;
      static citizen new_citizen() { return citizen(); }
      
    private:
      class CountryB_Citizen
      {
      public:
        CountryB_Citizen() {}
        std::string greeting() { return "Oi, oi! What's the story, mate?"; }
      };
    };
    
    int main(void)
    {
      CountryA::citizen a = CountryA::new_citizen();
      CountryB::citizen b = CountryB::new_citizen();
      
      std::cout << "In CountryA they say: '" << a.greeting() << "'" << std::endl;
      std::cout << "In CountryB they say: '" << b.greeting() << "'" << std::endl;  
    }
    So, as a user, all I have to know is that any given country will have a citizen, and that all citizens have a greeting.

    But from the developer's standpoint (ours), the internals of that citizen can work any way we please, as long as they still match the general interface. We can abstract away all of the side-effects that the user shouldn't be concerned with, and just give them the functionality they are looking for.

    And generally nested classes aren't too confusing. They can be debugged pretty much as easily as any other part of the class.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    nested classes are absolutely allowed in C++. in order to access them from outside, you have to use the fully qualified name.

    Code:
    class x
    {
    public:
      class y
      {
    
      };
    
      y bar;
    };
    
    x myX;
    x::y& bar = myX.bar;
    perfectly acceptable code.

    when and where to use it is another story altogether though.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A list/tree class might consist of the list/tree class with a nested node class that contains the data and pointers. The main list/tree class would then have a pointer to the root node or first/last nodes and the member functions to operate on the internal nodes (insertion/searching/deletion/etc.).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. What is the need of nested Class?
    By Alexpo in forum C++ Programming
    Replies: 4
    Last Post: 07-04-2008, 09:02 AM
  3. nested class question
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2007, 03:29 PM
  4. Returning pointer to a nested class
    By stingerkiss in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2006, 03:29 PM
  5. nested class as friend
    By c++isugly in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 05:36 PM

Tags for this Thread