A constructor question

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

    A constructor question

    I have a simple problem which I don't know how to handle. I have two
    classes, X and Y, and in each I have a constructor which takes a reference
    to the other as an argument as sketched below.

    class X {
    private:
    int a;
    public:
    X();
    X(Y&);
    };


    class Y {
    private:
    int b;
    public:
    Y();
    Y(X&);
    };


    Naturally, the compiler complains because Y is undeclared when it parses the
    definition of class X.

    Any help is appreciated.

    -----
    Jorn Attermann,


  • Ron Natalie

    #2
    Re: A constructor question


    "Jorn Attermann" <jornat@nospam. dk> wrote in message news:3f8c4e5c$0 $54792$edfadb0f @dread11.news.t ele.dk...[color=blue]
    > I have a simple problem which I don't know how to handle. I have two
    > classes, X and Y, and in each I have a constructor which takes a reference
    > to the other as an argument as sketched below.
    >[/color]
    add
    class Y;
    before the definition of X and vice versa.




    Comment

    • grejdanospam@pacbell.net

      #3
      Re: A constructor question

      On Tue, 14 Oct 2003 21:27:38 +0200, Jorn Attermann <jornat@nospam. dk>
      wrote:
      [color=blue]
      > I have a simple problem which I don't know how to handle. I have two
      > classes, X and Y, and in each I have a constructor which takes a
      > reference
      > to the other as an argument as sketched below.
      >
      > class X {
      > private:
      > int a;
      > public:
      > X();
      > X(Y&);
      > };
      >
      >
      > class Y {
      > private:
      > int b;
      > public:
      > Y();
      > Y(X&);
      > };
      >
      >
      > Naturally, the compiler complains because Y is undeclared when it parses
      > the
      > definition of class X.
      >
      > Any help is appreciated.
      >
      > -----
      > Jorn Attermann,
      >
      >
      >[/color]


      Forward declaration is needed.

      class X;

      class Y { declarations/ definitions ... };
      class X { declarations/defintiions ... };

      If , let's say , you want to use X as a member of Y , then X should be
      defined first.
      If reverse then reverse.
      It's nonsense to have type X member in Y and type Y member in X at the
      same type.



      --
      grzegorz

      Comment

      • jeffc

        #4
        Re: A constructor question


        <grejdanospam@p acbell.net> wrote in message
        news:oprw1srfle 6u0rcr@news.sf. sbcglobal.net.. .[color=blue]
        >
        > If , let's say , you want to use X as a member of Y , then X should be
        > defined first.
        > If reverse then reverse.
        > It's nonsense to have type X member in Y and type Y member in X at the
        > same type.[/color]

        Not necessarily. I might put a box in car, or I might put a car in a box.
        I might have a record that has a container, or I might have a container that
        has a record.


        Comment

        • Rolf Magnus

          #5
          Re: A constructor question

          jeffc wrote:
          [color=blue]
          >
          > <grejdanospam@p acbell.net> wrote in message
          > news:oprw1srfle 6u0rcr@news.sf. sbcglobal.net.. .[color=green]
          >>
          >> If , let's say , you want to use X as a member of Y , then X should
          >> be
          >> defined first.
          >> If reverse then reverse.
          >> It's nonsense to have type X member in Y and type Y member in X at
          >> the same type.[/color]
          >
          > Not necessarily.[/color]

          Yes, necessarily.
          [color=blue]
          > I might put a box in car, or I might put a car in a box.[/color]

          Right. The important part is _or_. Not every box contains a car and not
          every car contains a box (that in turn would then contain a car, which
          would contain a box, which .... you get the point).
          [color=blue]
          > I might have a record that has a container, or I might have a
          > container that has a record.[/color]


          Comment

          • jeffc

            #6
            Re: A constructor question


            "Rolf Magnus" <ramagnus@t-online.de> wrote in message
            news:bmi0ti$rvb $02$2@news.t-online.com...[color=blue]
            > jeffc wrote:
            >[color=green]
            > >
            > > <grejdanospam@p acbell.net> wrote in message
            > > news:oprw1srfle 6u0rcr@news.sf. sbcglobal.net.. .[color=darkred]
            > >>
            > >> If , let's say , you want to use X as a member of Y , then X should
            > >> be
            > >> defined first.
            > >> If reverse then reverse.
            > >> It's nonsense to have type X member in Y and type Y member in X at
            > >> the same type.[/color]
            > >
            > > Not necessarily.[/color]
            >
            > Yes, necessarily.
            >[color=green]
            > > I might put a box in car, or I might put a car in a box.[/color]
            >
            > Right. The important part is _or_. Not every box contains a car and not
            > every car contains a box (that in turn would then contain a car, which
            > would contain a box, which .... you get the point).[/color]

            Who says they have to be mutually exclusive? Let's just say for sake of
            argument that all cars are normal size, but box size varies greatly. All
            cars *can* have a box in them, and all boxes *can* have a car in them.

            class Box;

            class Car
            {
            private:
            Box* b;
            public:
            Car():b(0) {}
            Car(Box& box):b(&box){}
            };

            class Box
            {
            private:
            int cubicFeet;
            Car* c;
            public:
            Box(int size):cubicFeet (size) {}
            Box(Car& car, int size):cubicFeet (size), c(&car){}
            };

            int main()
            {
            Car car1;
            Box automobileShipp ingCrate(car1, 250);
            Box containerForThi ngs(1);
            Car car2(containerF orThings);

            return 0;
            }


            Comment

            • jeffc

              #7
              Re: A constructor question


              "Rolf Magnus" <ramagnus@t-online.de> wrote in message
              news:bmi0ti$rvb $02$2@news.t-online.com...[color=blue]
              >
              > Right. The important part is _or_. Not every box contains a car and not
              > every car contains a box (that in turn would then contain a car, which
              > would contain a box, which .... you get the point).[/color]

              grejdan said " It's nonsense to have type X member in Y and type Y member in
              X at the same type."
              Maybe he meant at the same "time". If so I see your point - yes, you can't
              do a recursive inclusion like that.


              Comment

              • Karl Heinz Buchegger

                #8
                Re: A constructor question



                grejdanospam@pa cbell.net wrote:[color=blue]
                >[/color]
                [color=blue]
                > If , let's say , you want to use X as a member of Y , then X should be
                > defined first.
                > If reverse then reverse.
                > It's nonsense to have type X member in Y and type Y member in X at the
                > same type.[/color]

                Right.
                But this is not what the OP is doing.
                The OP is doing the equivalent of:
                Every parent knows about its child and
                every child knows about its parent.

                --
                Karl Heinz Buchegger
                kbuchegg@gascad .at

                Comment

                • Rolf Magnus

                  #9
                  Re: A constructor question

                  jeffc wrote:
                  [color=blue]
                  >
                  > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                  > news:bmi0ti$rvb $02$2@news.t-online.com...[color=green]
                  >>
                  >> Right. The important part is _or_. Not every box contains a car and
                  >> not every car contains a box (that in turn would then contain a car,
                  >> which would contain a box, which .... you get the point).[/color]
                  >
                  > grejdan said " It's nonsense to have type X member in Y and type Y
                  > member in X at the same type."
                  > Maybe he meant at the same "time".[/color]

                  That's what I read anyway :-)
                  [color=blue]
                  > If so I see your point - yes, you can't do a recursive inclusion like
                  > that.[/color]

                  I don't know if he meant that, but at least it was my point.

                  Comment

                  • jeffc

                    #10
                    Re: A constructor question


                    "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                    news:3F8D14DA.D 0E354DE@gascad. at...[color=blue]
                    >
                    >
                    > grejdanospam@pa cbell.net wrote:[color=green]
                    > >[/color]
                    >[color=green]
                    > > If , let's say , you want to use X as a member of Y , then X should be
                    > > defined first.
                    > > If reverse then reverse.
                    > > It's nonsense to have type X member in Y and type Y member in X at the
                    > > same type.[/color]
                    >
                    > Right.
                    > But this is not what the OP is doing.
                    > The OP is doing the equivalent of:
                    > Every parent knows about its child and
                    > every child knows about its parent.[/color]

                    Huh? I don't understand your terminology. There is no subclassing involved
                    here.


                    Comment

                    • Jorn Attermann

                      #11
                      Re: A constructor question


                      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
                      news:3F8D14DA.D 0E354DE@gascad. at...[color=blue]
                      >
                      >
                      > grejdanospam@pa cbell.net wrote:[color=green]
                      > >[/color]
                      >[color=green]
                      > > If , let's say , you want to use X as a member of Y , then X should be
                      > > defined first.
                      > > If reverse then reverse.
                      > > It's nonsense to have type X member in Y and type Y member in X at the
                      > > same type.[/color]
                      >
                      > Right.
                      > But this is not what the OP is doing.
                      > The OP is doing the equivalent of:
                      > Every parent knows about its child and
                      > every child knows about its parent.
                      >
                      > --
                      > Karl Heinz Buchegger
                      > kbuchegg@gascad .at[/color]

                      I really appreciate the help from the group; it solved my problem. I also
                      find the other responses interesting and please let me explain what my
                      original problem was:

                      I have two classes (from Template Numerical Toolkit -
                      http://math.nist.gov/tnt/), an Array1D and an Array2D for numerical
                      computing. And I wanted to have a method for conversion between these. The
                      way I solved this was to include a constructor in each class which take as
                      argument a reference to the other class. So, there *is* actually a practical
                      use for it.

                      Thanks again,

                      Jorn Attermann


                      Comment

                      Working...