forward declaration?

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

    forward declaration?

    I have the following 2 classes:
    class A{
    B * b;
    ....
    };
    class B{
    A * a;
    ....
    };
    The problem is I cannot declare class A without declaring class B first, and
    vice versa, as each declares a variable of the other class. How do I solve
    this problem?

    Thanks in advance,
    Lem


  • Michael Demanet

    #2
    Re: forward declaration?

    Lem wrote:[color=blue]
    > I have the following 2 classes:
    > class A{
    > B * b;
    > ...
    > };
    > class B{
    > A * a;
    > ...
    > };
    > The problem is I cannot declare class A without declaring class B first, and
    > vice versa, as each declares a variable of the other class. How do I solve
    > this problem?
    >
    > Thanks in advance,
    > Lem
    >
    >[/color]

    why not using forward class definition

    try to append this in the header file
    class A;
    class B;
    before the declaration of the classes.

    Comment

    • Michael Demanet

      #3
      Re: forward declaration?

      Michael Demanet wrote:[color=blue]
      > why not using forward class definition[/color]
      I meant forward class DECLARATION sorry

      Michael

      Comment

      • Rob Williscroft

        #4
        Re: forward declaration?

        Lem wrote in news:bkk2g9$hhv $1@nobel.pacifi c.net.sg:
        [color=blue]
        > I have the following 2 classes:[/color]

        class B;

        The above declares B as a class-type, its an incomplete declaration so
        you can't do say: class C { B b; }; but you can declare pointers (as
        you want to below) refrences. The key is that you can't do anything
        with A that requires a *complete* (has a body {}) defenition of A,
        some examples bellow.
        [color=blue]
        > class A{
        > B * b;
        > ...
        > };
        > class B{
        > A * a;
        > ...
        > };
        > The problem is I cannot declare class A without declaring class B
        > first, and vice versa, as each declares a variable of the other class.
        > How do I solve this problem?
        >[/color]

        <example>
        #include <iostream>
        #include <ostream>

        class A;

        class C
        {
        public:
        A *pointer;
        A &reference;
        static A static_a;
        C( A &r ) : pointer( 0 ), reference( r ) {}
        };

        /* without extern this would require a complete declaration
        */
        extern A extern_a;

        /* This is only a declaration so its ok, we can't call it or
        give a *defenition* intill A is *complete*
        */
        void function( A a );

        class A
        {
        public:
        int a;
        };

        /* A is complete so we can now *define* things that need a
        *complete* defenition of A
        */
        A C::static_a = { 1 };
        A extern_a = { 2 };

        void function( A a )
        {
        std::cout << a.a << std::endl;
        }

        int main()
        {
        A a;
        C c(a);
        c.pointer = &a;

        a.a = 3;

        function( a );
        function( c.reference );

        function( C::static_a );

        function( extern_a );

        }
        </example>

        Rob.
        --

        Comment

        • osmium

          #5
          Re: forward declaration?


          Lem <hjlem@pacific. net.sg> wrote in message
          news:bkk2g9$hhv $1@nobel.pacifi c.net.sg...[color=blue]
          > I have the following 2 classes:
          > class A{
          > B * b;
          > ...
          > };
          > class B{
          > A * a;
          > ...
          > };
          > The problem is I cannot declare class A without declaring class B first,[/color]
          and[color=blue]
          > vice versa, as each declares a variable of the other class. How do I[/color]
          solve[color=blue]
          > this problem?
          >
          > Thanks in advance,
          > Lem
          >
          >[/color]


          Comment

          • osmium

            #6
            Re: forward declaration?

            Lem writes:
            [color=blue]
            > I have the following 2 classes:
            > class A{
            > B * b;
            > ...
            > };
            > class B{
            > A * a;
            > ...
            > };
            > The problem is I cannot declare class A without declaring class B first,[/color]
            and[color=blue]
            > vice versa, as each declares a variable of the other class. How do I[/color]
            solve[color=blue]
            > this problem?[/color]

            You can't solve what you have shown. You have infinite recursion, or
            something (I am not a linguist). An A contains a B and a B contains an A,
            and so on to infinity. There is not enough context there so we can deduce
            what you are trying to do. Just kind of flailing at what you want; do a
            forward declaration of B and use pointers or references. If that doesn't
            get what you want, explain what you want to accomplish in more detail.


            Comment

            • Fraser Ross

              #7
              Re: forward declaration?


              "osmium"[color=blue]
              > Lem writes:
              >[color=green]
              > > I have the following 2 classes:
              > > class A{
              > > B * b;
              > > ...
              > > };
              > > class B{
              > > A * a;
              > > ...
              > > };
              > > The problem is I cannot declare class A without declaring class B first,[/color]
              > and[color=green]
              > > vice versa, as each declares a variable of the other class. How do I[/color]
              > solve[color=green]
              > > this problem?[/color]
              >
              > You can't solve what you have shown. You have infinite recursion, or
              > something (I am not a linguist). An A contains a B and a B contains an A,
              > and so on to infinity. There is not enough context there so we can deduce
              > what you are trying to do. Just kind of flailing at what you want; do a
              > forward declaration of B and use pointers or references. If that doesn't
              > get what you want, explain what you want to accomplish in more detail.
              >
              >[/color]
              A contains a pointer to a B. Pointers have a known size and hence the code
              only needs to be told that B is a class:
              class A{
              class B * b;
              ...
              };

              Class B is accepted because A has been seen and its size is known.

              Fraser.



              ---
              Outgoing mail is certified Virus Free.
              Checked by AVG anti-virus system (http://www.grisoft.com).
              Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/2003


              Comment

              • Fraser Ross

                #8
                Re: forward declaration?


                "Fraser Ross"[color=blue]
                > Class B is accepted because A has been seen and its size is known.[/color]
                Actually B is accepted because A has been seen.

                Fraser.



                ---
                Outgoing mail is certified Virus Free.
                Checked by AVG anti-virus system (http://www.grisoft.com).
                Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/2003


                Comment

                • lallous

                  #9
                  Re: forward declaration?

                  Hello,

                  How to solve then this forward problem:

                  class A;
                  class B
                  {
                  public:
                  A *a;
                  void test()
                  {
                  int x = A::A1;
                  }
                  };

                  class A
                  {
                  public:
                  enum
                  {
                  A1,
                  A2
                  };
                  };


                  I need to access A's enum from class B.

                  Regards,
                  Elias
                  "Lem" <hjlem@pacific. net.sg> wrote in message
                  news:bkk2g9$hhv $1@nobel.pacifi c.net.sg...[color=blue]
                  > I have the following 2 classes:
                  > class A{
                  > B * b;
                  > ...
                  > };
                  > class B{
                  > A * a;
                  > ...
                  > };
                  > The problem is I cannot declare class A without declaring class B first,[/color]
                  and[color=blue]
                  > vice versa, as each declares a variable of the other class. How do I[/color]
                  solve[color=blue]
                  > this problem?
                  >
                  > Thanks in advance,
                  > Lem
                  >
                  >[/color]


                  Comment

                  • osmium

                    #10
                    Re: forward declaration?

                    Fraser Ross writes:
                    [color=blue]
                    > "osmium"[color=green]
                    > > Lem writes:
                    > >[color=darkred]
                    > > > I have the following 2 classes:
                    > > > class A{
                    > > > B * b;
                    > > > ...
                    > > > };
                    > > > class B{
                    > > > A * a;
                    > > > ...
                    > > > };
                    > > > The problem is I cannot declare class A without declaring class B[/color][/color][/color]
                    first,[color=blue][color=green]
                    > > and[color=darkred]
                    > > > vice versa, as each declares a variable of the other class. How do I[/color]
                    > > solve[color=darkred]
                    > > > this problem?[/color]
                    > >
                    > > You can't solve what you have shown. You have infinite recursion, or
                    > > something (I am not a linguist). An A contains a B and a B contains an[/color][/color]
                    A,[color=blue][color=green]
                    > > and so on to infinity. There is not enough context there so we can[/color][/color]
                    deduce[color=blue][color=green]
                    > > what you are trying to do. Just kind of flailing at what you want; do a
                    > > forward declaration of B and use pointers or references. If that[/color][/color]
                    doesn't[color=blue][color=green]
                    > > get what you want, explain what you want to accomplish in more detail.
                    > >
                    > >[/color]
                    > A contains a pointer to a B. Pointers have a known size and hence the[/color]
                    code[color=blue]
                    > only needs to be told that B is a class:
                    > class A{
                    > class B * b;
                    > ...
                    > };
                    >
                    > Class B is accepted because A has been seen and its size is known.[/color]

                    Oops! Sorry about that, I am so used to seeing what I thought I saw that I
                    saw it yet again.


                    Comment

                    • John Carson

                      #11
                      Re: forward declaration?

                      "lallous" <lallous@lgwm.o rg> wrote in message
                      news:bkkf6b$2op b8$1@ID-161723.news.uni-berlin.de[color=blue]
                      > Hello,
                      >
                      > How to solve then this forward problem:
                      >
                      > class A;
                      > class B
                      > {
                      > public:
                      > A *a;
                      > void test()
                      > {
                      > int x = A::A1;
                      > }
                      > };
                      >
                      > class A
                      > {
                      > public:
                      > enum
                      > {
                      > A1,
                      > A2
                      > };
                      > };
                      >
                      >
                      > I need to access A's enum from class B.
                      >
                      > Regards,
                      > Elias[/color]


                      Only declare test() in the class declaration. Define it later.


                      class A;
                      class B
                      {
                      public:
                      A *a;
                      void test();
                      };

                      class A
                      {
                      public:
                      enum
                      {
                      A1,
                      A2
                      };
                      };


                      inline void B::test()
                      {
                      int x = A::A1;
                      }



                      --
                      John Carson
                      1. To reply to email address, remove donald
                      2. Don't reply to email address (post here instead)

                      Comment

                      • jeffc

                        #12
                        Re: forward declaration?


                        "osmium" <r124c4u102@com cast.net> wrote in message
                        news:bkk6fl$2n7 sj$1@ID-179017.news.uni-berlin.de...[color=blue]
                        >
                        > You can't solve what you have shown. You have infinite recursion, or
                        > something (I am not a linguist). An A contains a B and a B contains an[/color]
                        A...

                        No, not really. A contains a ptr to a B, which might be zero, or it might
                        point to a B which has a 0 ptr to an A, etc.


                        Comment

                        • jeffc

                          #13
                          Re: forward declaration?


                          "lallous" <lallous@lgwm.o rg> wrote in message
                          news:bkkf6b$2op b8$1@ID-161723.news.uni-berlin.de...[color=blue]
                          > Hello,
                          >
                          > How to solve then this forward problem:
                          >
                          > class A;
                          > class B
                          > {
                          > public:
                          > A *a;
                          > void test()
                          > {
                          > int x = A::A1;
                          > }
                          > };
                          >
                          > class A
                          > {
                          > public:
                          > enum
                          > {
                          > A1,
                          > A2
                          > };
                          > };
                          >
                          >
                          > I need to access A's enum from class B.[/color]

                          There is no similar problem here. Just define A before B. A has no
                          reference to B, so obviously it needs to come first.


                          Comment

                          Working...