Mutually referential classes

Discussion in 'C++' started by Colin JN Breame, Jan 21, 2004.

  1. Say there are two classes:

    class A {
    B *b;
    };

    class B {
    A *a;
    };

    How can this be defined and compiled?

    Thanks
     
    Colin JN Breame, Jan 21, 2004
    #1
    1. Advertising

  2. Colin JN Breame

    Sumit Rajan Guest

    "Colin JN Breame" <> wrote in message
    news:p...
    > Say there are two classes:



    class B;

    > class A {
    > B *b;
    > };
    >
    > class B {
    > A *a;
    > };
    >
    > How can this be defined and compiled?
    >
    > Thanks
     
    Sumit Rajan, Jan 21, 2004
    #2
    1. Advertising

  3. Colin JN Breame

    The Flib Guest

    Colin JN Breame <> wrote in message news:<>...
    > Say there are two classes:
    >
    > class A {
    > B *b;
    > };
    >
    > class B {
    > A *a;
    > };
    >
    > How can this be defined and compiled?
    >
    > Thanks


    Heya. What you need to do is stick a forward declaration of B before
    you define A. The compiler is way smarter than we mere humans, so it
    figures out what you mean when you say:

    class B;

    class A
    {
    B *b;
    };

    class B
    {
    A *a;
    }
     
    The Flib, Jan 21, 2004
    #3
  4. Colin JN Breame

    David Harmon Guest

    On Thu, 22 Jan 2004 01:42:00 +0530 in comp.lang.c++, "Sumit Rajan"
    <> was alleged to have written:
    >> Say there are two classes:


    This issue is covered in Marshall Cline's C++ FAQ. See the topic
    "[38.11] How can I create two classes that both know about each other?"
    It is always good to check the FAQ before posting. You can get the FAQ
    at:
    http://www.parashift.com/c -faq-lite/
     
    David Harmon, Jan 21, 2004
    #4
  5. Colin JN Breame

    c++novice Guest

    Colin JN Breame <> wrote in message news:<>...
    > Say there are two classes:
    >
    > class A {
    > B *b;
    > };
    >
    > class B {
    > A *a;
    > };
    >
    > How can this be defined and compiled?
    >
    > Thanks


    Do a forward declaration--

    Class B; //declared

    class A {
    B *b;
    };

    class B { //defined
    A *a;
    };
     
    c++novice, Jan 22, 2004
    #5
  6. On Wed, 21 Jan 2004 23:53:02 +0000, David Harmon wrote:

    > On Thu, 22 Jan 2004 01:42:00 +0530 in comp.lang.c++, "Sumit Rajan"
    > <> was alleged to have written:
    >>> Say there are two classes:

    >
    > This issue is covered in Marshall Cline's C++ FAQ. See the topic "[38.11]
    > How can I create two classes that both know about each other?" It is
    > always good to check the FAQ before posting. You can get the FAQ at:
    > http://www.parashift.com/c -faq-lite/


    Oh yes, should have checked.

    Thanks anyway!
     
    Colin JN Breame, Jan 22, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad Kartchner
    Replies:
    10
    Views:
    615
    Walter Tross
    May 1, 2004
  2. Eric

    Re: Mutually referential types

    Eric, Aug 15, 2003, in forum: C Programming
    Replies:
    5
    Views:
    354
    Mark Haigh
    Aug 17, 2003
  3. Chris Torek

    Re: Mutually referential types

    Chris Torek, Aug 15, 2003, in forum: C Programming
    Replies:
    1
    Views:
    309
    Colin JN Breame
    Aug 16, 2003
  4. G Patel

    mutually referential (Pg 140 K&R2)

    G Patel, Apr 23, 2005, in forum: C Programming
    Replies:
    14
    Views:
    665
  5. jrwats
    Replies:
    6
    Views:
    347
    Zachary Turner
    Jan 18, 2009
Loading...

Share This Page