Circular references involving internal classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Rudolf

    Circular references involving internal classes

    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




  • Chris \( Val \)

    #2
    Re: Circular references involving internal classes


    "Dave Rudolf" <nuclearwhippin gboy@hotmail.co m> wrote in message
    news:104fuhe7ct 02o21@corp.supe rnews.com...
    | 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


    Comment

    • Rob Williscroft

      #3
      Re: Circular references involving internal classes

      Dave Rudolf wrote in news:104fuhe7ct 02o21@corp.supe rnews.com:
      [color=blue]
      > 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.[/color]

      No, though you can do this:

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

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

      [color=blue]
      > 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
      > {[/color]

      This is illegal too, B is an *incomplete* type, you can declare
      pointers and references to B (B*, B& etc) but not B's.
      [color=blue]
      > B _b;
      > // B::Bi _bi;
      > };
      >[/color]
      [snip]

      Rob.
      --

      Comment

      Working...