Recursive Data Structure

This is a discussion on Recursive Data Structure within the C++ Programming forums, part of the General Programming Boards category; Hi. Pardon me if this is an obvious one... Is there a way to get around C++'s refusal to have ...

  1. #1
    external validation
    Join Date
    Dec 2004
    Posts
    9

    Recursive Data Structure

    Hi.
    Pardon me if this is an obvious one...
    Is there a way to get around C++'s refusal to have a class member that's an instance of the class itself...?
    What I'd like is:
    Code:
    class MyClass {
      int myClassMember01;
      struct myStruct {
        int myStructMember01;
        MyClass myStructMember02;
      } myClassMember02;
    }; // End declaration of MyClass
    But what is allowed is:
    Code:
    class MyClass {
      int myClassMember01;
      struct myStruct {
        int myStructMember01;
        MyClass & myStructMember02;
      } myClassMember02;
    }; // End declaration of MyClass
    I am writing a model of a hardware architecture and it'd be conceptually much more realistic if I could use the first of the two above.
    Any suggestions?
    Is it in fact true to say that the first example is just conceptually impossible in C++?
    Cheers.
    Last edited by dwylie; 08-02-2005 at 07:00 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >Is it in fact true to say that the first example is just conceptually impossible in C++?

    Yes.

    In fact, I don't think you can use a reference to an object of class A in class A, either, since using a reference implies that a full, valid, constructed object is available somewhere that you are referencing and that isn't possible because you don't know what the full class is yet to even make the original object.

    You can use a pointer to an object of class A in class A, however, but you probably already know that.
    You're only born perfect.

  3. #3
    external validation
    Join Date
    Dec 2004
    Posts
    9
    Thanks elad.
    Good to hear someone agree that the former option is impossible so I can just get on with the second without nagging doubts that I should be further investigating the first.
    The use of a reference to class A inside class A compiles ok - we'll see how it runs...

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The reference would be fine, but remember, references must be initialized to an object to refer to. That is, unlike a pointer, you can't change where they point. So basically, you have to set it in the initializer list. However, some instance of your class must be created first, and this one will have an invalid reference. So, use pointers.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    external validation
    Join Date
    Dec 2004
    Posts
    9
    Yep.
    Just figured that out.
    You need an instance of the class, to initialize the reference, but you can't get an instance of the class until you've initialised the reference.
    Cool.
    Cheers,
    D.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Thanks elad.
    Good to hear someone agree that the former option is impossible
    Do you see why it's impossible? To create an object of the class, the compiler first has to create the member object. But the member object itself contains an object of the class, so the compiler has to first create the member's member object....etc., etc. on out to infinity.

    If instead, the member is a pointer to the class, then to create an object of the class, the compiler just has to create a space in memory that can hold the address of a class object. That's where things stop---there is no infinite chain. A pointer is not an object, and it does not have any member variables that need to be created.

    Then, after you create at least two objects, you can assign the address of one of the objects to the pointer member variable of the other object.

  7. #7
    external validation
    Join Date
    Dec 2004
    Posts
    9
    Quote Originally Posted by 7stud
    Do you see why it's impossible? ...<snip>...
    Yeah.
    Figured it out.
    Cheers my friend.
    D.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  2. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  3. Appropriate data structure
    By gazsux in forum Game Programming
    Replies: 3
    Last Post: 03-19-2003, 12:26 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM

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