Thread: Nesting classes

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105

    Nesting classes

    According to my C++ book, the following is legal:
    Code:
    class a_class
    {
              public:
                        int one;
                        int two;
    };
    
    class b_class
    {
             public:
                       int fortytwo;
                       a_class b_a;
    };
    
    b_class b;
    How do I reference the members of an a_class object that is naested inside of a b_class object? Would the following be legal?
    Code:
    b.b_a.one;
    Hatman Approves!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Precisely.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Not the best use of the word nest/nested/nesting. Any programmer would have thought you meant

    Code:
    class A { class B { }; };
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Quote Originally Posted by DarkAlex View Post
    According to my C++ book, the following is legal:
    Code:
    class a_class
    {
              public:
                        int one;
                        int two;
    };
    
    class b_class
    {
             public:
                       int fortytwo;
                       a_class b_a;
    };
    
    b_class b;
    The example you gave would be defined as containment.
    class b contains an instance of class a in other words.
    Nesting would be defining class a in-side of class b as the post above examples.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    Free Country, USA
    Posts
    105
    Ok. Thanks for the help, and for clearing up the terms. I'l be sure to use the term 'containment' to describe this situation from now on.
    Last edited by DarkAlex; 03-29-2009 at 09:08 PM. Reason: Mispelled word
    Hatman Approves!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM