Circular references involving internal classes

Discussion in 'C++' started by Dave Rudolf, Mar 5, 2004.

  1. Dave Rudolf

    Dave Rudolf Guest

    Hey all,

    I have a philosophical question for you folks -- that is, I don't really
    need an immediate solution, but rather I am just curious.

    Is there some way to make forward declarations for internal classes. The
    situlation that I have is probably best illustrated with some code:


    // legal forward declaration
    class B;


    // illegal forward declaration
    // class B::Bi;

    class A
    {
    B _b;
    // B::Bi _bi;
    };


    class B
    {
    class Bi
    {
    };

    A _a;
    };


    Let's forget that the above code is probably not the best design. Now, if I
    want class A to handle references to class B, I can do the normal forward
    declaration thing before class A, as above. But what if I want to reference
    B's inner class, as suggested by the commented-out code? Is there any way to
    do such a thing?

    Of course, the work-around could be to move the inner class into it's own
    class. Like I said, I'm just curious.

    Dave
    Dave Rudolf, Mar 5, 2004
    #1
    1. Advertising

  2. "Dave Rudolf" <> wrote in message
    news:...
    | Hey all,
    |
    | I have a philosophical question for you folks -- that is, I don't really
    | need an immediate solution, but rather I am just curious.
    |
    | Is there some way to make forward declarations for internal classes. The
    | situlation that I have is probably best illustrated with some code:
    |
    |
    | // legal forward declaration
    | class B;
    |
    |
    | // illegal forward declaration
    | // class B::Bi;
    |
    | class A
    | {
    | B _b;
    | // B::Bi _bi;
    | };
    |
    |
    | class B
    | {
    | class Bi
    | {
    | };
    |
    | A _a;
    | };
    |
    |
    | Let's forget that the above code is probably not the best design. Now, if I
    | want class A to handle references to class B, I can do the normal forward
    | declaration thing before class A, as above. But what if I want to reference
    | B's inner class, as suggested by the commented-out code? Is there any way to
    | do such a thing?
    |
    | Of course, the work-around could be to move the inner class into it's own
    | class. Like I said, I'm just curious.

    Use a pointer to the objects:

    class B;
    class Bi;

    class A
    {
    B* _b;
    Bi* _bi;
    };

    Cheers.
    Chris Val
    Chris \( Val \), Mar 5, 2004
    #2
    1. Advertising

  3. Dave Rudolf wrote in news::

    > Hey all,
    >
    > I have a philosophical question for you folks -- that is, I don't
    > really need an immediate solution, but rather I am just curious.
    >
    > Is there some way to make forward declarations for internal classes.


    No, though you can do this:

    class Outer
    {
    class Inner; /* kinda forward declare */
    };

    class Outer::Inner /* define */
    {
    };


    > The situlation that I have is probably best illustrated with some
    > code:
    >
    >
    > // legal forward declaration
    > class B;
    >
    >
    > // illegal forward declaration
    > // class B::Bi;
    >
    > class A
    > {


    This is illegal too, B is an *incomplete* type, you can declare
    pointers and references to B (B*, B& etc) but not B's.

    > B _b;
    > // B::Bi _bi;
    > };
    >

    [snip]

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Mar 5, 2004
    #3
    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. Dan C Douglas

    Circular references are not supported

    Dan C Douglas, Aug 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,399
    Steve C. Orr, MCSD
    Aug 13, 2003
  2. Bill Jones

    Re: Circular References

    Bill Jones, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    =?Utf-8?B?RGF2ZQ==?=
    Apr 8, 2004
  3. Kiuhnm
    Replies:
    16
    Views:
    722
    Jonathan Mcdougall
    Jan 3, 2005
  4. Ethan Furman

    internal circular class references

    Ethan Furman, Dec 10, 2008, in forum: Python
    Replies:
    8
    Views:
    353
    Ethan Furman
    Dec 11, 2008
  5. , India
    Replies:
    2
    Views:
    399
    Robert Fendt
    Apr 18, 2010
Loading...

Share This Page