Confused about nested structs/classes

This is a discussion on Confused about nested structs/classes within the C++ Programming forums, part of the General Programming Boards category; Look at the code below. When the Hen object is created, is a Nest and Egg created inside of it? ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    32

    Confused about nested structs/classes

    Look at the code below. When the Hen object is created, is a Nest and Egg created inside of it? Or are the structs independent apart from scope? It seems to be so when it is allowed to create an Egg object.

    I've looked around on the web but have not been able to find information about nesting that explains this. I hope that someone in this forum could clarify or point me to a tutorial.

    Seron

    Code:
    // henegg.cpp
    // exercise 5.6
    
    #include <iostream>
    using namespace std;
    
    struct Hen {
    	
      struct Nest {
      	
        struct Egg {
      		
          void display();
        };
      	
        void display();
      };
      
      void display();
    };
    
    void Hen::Nest::Egg::display() {
    	
      cout << "Egg" << endl;
    }
    
    void Hen::Nest::display() {
    	
      cout << "Nest" << endl;
    }
    
    void Hen::display() {
    	
      cout << "Hen" << endl;
    }
    
    void main() {
    	
      Hen h;
      Hen::Nest n;
      Hen::Nest::Egg e;
      
      h.display();
      n.display();
      e.display();
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >When the Hen object is created, is a Nest and Egg created inside of it?

    No, there's no instances of Nest or Egg within Hen. Only their declarations.

    >Or are the structs independent apart from scope?

    Yes, you can create and use any one of the three without the existence of the others.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    if you need class interdependancy, you would have the class declaration and their instance seperate, [and in sequence], since their data functions would require known instantiated handles of the dependant classes. it would be useful to have the declarations in a seperate header, so you can include this in any dependant class before you define that class' member functions.

    however, this is generally difficult to manage, and makes your code less forward portable. usually you should make your project's files as independant of previous declarations as possible as you nest user defined conglomerate types, as well as source files. keep your modules seperated as much as possible.
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Problem constructing a class with a nested struc
    By pliang in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 07:43 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 09:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21