two class definitions which uses each other

Discussion in 'C++' started by sdf, Jun 16, 2008.

  1. sdf

    sdf Guest

    Hi.

    Sorry for lame question, but I'm puzzled.
    What if I have:

    class A
    {
    public:
    class B some_method ();
    };

    and

    class B
    {
    public:
    class A some_value
    };

    Class definitions are separated to different header files. And
    compiler (VS2008 and mingw checked both) cannot compile class A before
    class B compiled.

    So what is correct way to handle such situation?
    sdf, Jun 16, 2008
    #1
    1. Advertising

  2. sdf

    Jim Langston Guest

    "sdf" <> wrote in message
    news:...
    > Hi.
    >
    > Sorry for lame question, but I'm puzzled.
    > What if I have:
    >
    > class A
    > {
    > public:
    > class B some_method ();
    > };
    >
    > and
    >
    > class B
    > {
    > public:
    > class A some_value
    > };
    >
    > Class definitions are separated to different header files. And
    > compiler (VS2008 and mingw checked both) cannot compile class A before
    > class B compiled.
    >
    > So what is correct way to handle such situation?


    You can't really do that. One way is to have class A some_method return a
    pointer to class B, then class A doesn't have to know what class B looks
    like, just that it exists:

    class B;

    class A
    {
    public:
    B* some_method();
    };

    class B
    {
    public
    A some_value;
    };

    As long as it's returning a value from a method/function and not containing
    the class. If class A contained class B which contained class A, the size
    of either of the classes would be infiinite.
    Jim Langston, Jun 17, 2008
    #2
    1. Advertising

  3. sdf

    metarox Guest

    What you have here is called a cyclic dependency. Google it and you
    will get tons of items.

    A class must know its size at compile-time, thus it must know the size
    of all its internals like its attributes.

    You can also use the Pimpl Idiom (Opaque Pointer) technique to resolve
    the problem.

    David
    metarox, Jun 17, 2008
    #3
  4. On 2008-06-17 04:06, metarox wrote:
    > What you have here is called a cyclic dependency. Google it and you
    > will get tons of items.
    >
    > A class must know its size at compile-time, thus it must know the size
    > of all its internals like its attributes.
    >
    > You can also use the Pimpl Idiom (Opaque Pointer) technique to resolve
    > the problem.


    You do not have to go that far, a simple pointer will do, since the size
    of a pointer is known:

    class B; // forward declaration

    class A {
    B* b;
    };

    class B {
    A a;
    };

    --
    Erik Wikström
    Erik Wikström, Jun 17, 2008
    #4
    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. Alex Ochoa
    Replies:
    5
    Views:
    387
  2. Replies:
    7
    Views:
    3,694
    John Currier
    Sep 4, 2005
  3. cppaddict
    Replies:
    4
    Views:
    453
    jeffc
    Oct 15, 2003
  4. Replies:
    5
    Views:
    411
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 16, 2007
  5. kunjaan
    Replies:
    8
    Views:
    107
    Charles Oliver Nutter
    May 28, 2009
Loading...

Share This Page