operation overloading headache

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    operation overloading headache

    The code below I suspect creates memory leaks:
    In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object.
    After "c += 'new_object'"
    we have an unreferenced object which created from "a + b" and not destroyed.
    am I right?
    What I can do to avoid this?
    ------------------------------
    class Vector3D {
    protected:
    float _x, _y, _z;
    public:
    Vector3D(float x, float y, float z) : _x(x), _y(y), _z(z) {}
    const Vector3D &operator+=(con st Vector3D &a);
    friend const Vector3D operator+(const Vector3D &a, const Vector3D &b);
    };

    const Vector3D &Vector3D::oper ator+=(const Vector3D &a) {
    _x += a._x; _y += a._y; _z += a._z; return *this;
    }
    const Vector3D operator+(const Vector3D &a, const Vector3D &b) {
    return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z);
    }

    void main() {
    Vector3D a = Vector3D(1, 2, 3);
    Vector3D b = Vector3D(4, 5, 6);
    Vector3D c = Vector3D(0, 0, 0);
    c += a + b;
    }


  • Guest's Avatar

    #2
    Re: operatOR overloading headache

    Not OperatION, operatOR. Sorry!
    [color=blue]
    > The code below I suspect creates memory leaks:
    > In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object.
    > After "c += 'new_object'"
    > we have an unreferenced object which created from "a + b" and not destroyed.
    > am I right?
    > What I can do to avoid this?
    > ------------------------------
    > class Vector3D {
    > protected:
    > float _x, _y, _z;
    > public:
    > Vector3D(float x, float y, float z) : _x(x), _y(y), _z(z) {}
    > const Vector3D &operator+=(con st Vector3D &a);
    > friend const Vector3D operator+(const Vector3D &a, const Vector3D &b);
    > };
    >
    > const Vector3D &Vector3D::oper ator+=(const Vector3D &a) {
    > _x += a._x; _y += a._y; _z += a._z; return *this;
    > }
    > const Vector3D operator+(const Vector3D &a, const Vector3D &b) {
    > return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z);
    > }
    >
    > void main() {
    > Vector3D a = Vector3D(1, 2, 3);
    > Vector3D b = Vector3D(4, 5, 6);
    > Vector3D c = Vector3D(0, 0, 0);
    > c += a + b;
    > }
    >
    >[/color]


    Comment

    • Peter van Merkerk

      #3
      Re: operation overloading headache

      "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
      news:bki555$nik $1@nic.grnet.gr ...[color=blue]
      > The code below I suspect creates memory leaks:
      > In the main() in line "c += a + b;" when compiler runs "a + b" creates a[/color]
      new object.[color=blue]
      > After "c += 'new_object'"
      > we have an unreferenced object which created from "a + b" and not[/color]
      destroyed.[color=blue]
      > am I right?[/color]

      No. You don't have to worry about a memory leak; temporary objects will be
      automatically destroyed after the expression which caused them to be created
      has been fully evaluated.

      --
      Peter van Merkerk
      peter.van.merke rk(at)dse.nl




      Comment

      • Alf P. Steinbach

        #4
        Re: operation overloading headache

        On Sat, 20 Sep 2003 21:06:41 +0300, "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote:
        [color=blue]
        >The code below I suspect creates memory leaks:[/color]

        Nope.

        But it's prone to have other bugs since you don't use indentation.


        [color=blue]
        >In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object.[/color]

        The new object (if any, this depends on what optimizations the compiler
        throws in; read about RVO) is a temporary one, on the stack, and it's
        discarded after its value is assigned to 'c'.


        [color=blue]
        >After "c += 'new_object'"
        >we have an unreferenced object which created from "a + b" and not destroyed.
        >am I right?[/color]

        No.

        [color=blue]
        >What I can do to avoid this?[/color]

        Write correct code.

        [color=blue]
        >------------------------------
        >class Vector3D {
        >protected:
        >float _x, _y, _z;[/color]

        Don't use leading underscores. In C++ names beginning with leading
        underscore followed by uppercase letter are reserved for the
        implementation. In C there are even more restrictions.

        If you don't have a lot of vectors at the same time, consider using
        'double' instead of 'float'. 'double' has better precision and on
        modern machines is as fast as, or even faster than, 'float'.

        [color=blue]
        >public:
        >Vector3D(flo at x, float y, float z) : _x(x), _y(y), _z(z) {}[/color]

        Do use indentation, and also some blank lines.

        [color=blue]
        >const Vector3D &operator+=(con st Vector3D &a);[/color]


        [color=blue]
        >friend const Vector3D operator+(const Vector3D &a, const Vector3D &b);
        >};
        >
        >const Vector3D &Vector3D::oper ator+=(const Vector3D &a) {
        >_x += a._x; _y += a._y; _z += a._z; return *this;
        >}[/color]

        OK except for formatting and names.

        [color=blue]
        >const Vector3D operator+(const Vector3D &a, const Vector3D &b) {
        >return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z);
        >}[/color]

        OK except for formatting and names.


        [color=blue]
        >void main() {[/color]

        Non-standard extension. Use a standard 'main'.

        [color=blue]
        >Vector3D a = Vector3D(1, 2, 3);
        >Vector3D b = Vector3D(4, 5, 6);
        >Vector3D c = Vector3D(0, 0, 0);
        >c += a + b;
        >}[/color]


        To be honest, Vector3D looks like someone else's code reformatted.

        Comment

        • Guest's Avatar

          #5
          Re: operation overloading headache

          > >The code below I suspect creates memory leaks:[color=blue]
          > Nope.
          > But it's prone to have other bugs since you don't use indentation.[/color]

          Thanks for your response.
          What is indentation? I don't understand the word ;-(
          [color=blue][color=green]
          > >What I can do to avoid this?[/color]
          >
          > Write correct code.[/color]

          lol ;-)
          [color=blue]
          > Do use indentation, and also some blank lines.
          > OK except for formatting and names.[/color]

          I "pack" the code to avoid a big message.
          [color=blue][color=green]
          > >Vector3D a = Vector3D(1, 2, 3);
          > >Vector3D b = Vector3D(4, 5, 6);
          > >Vector3D c = Vector3D(0, 0, 0);
          > >c += a + b;
          > >}[/color]
          > To be honest, Vector3D looks like someone else's code reformatted.[/color]

          grrrrrr! of course its mine! I am trying to test operator overloading because I read "Thinking in C++"
          Because I use OpenGL, I am trying to write (what else) a Vector3D class


          Comment

          • Agent Mulder

            #6
            Re: operation overloading headache

            <Alf P. Steinbach>[color=blue]
            >To be honest, Vector3D looks like someone else's code reformatted.[/color]
            </>

            That's a good way to learn. I recommend it to every student. Reformat
            someone else's code.

            <- Chameleon ->
            What is indentation? I don't understand the word ;-(
            </>

            It's an old C++ joke: "The road to perdition is paved with good
            indentation". It means that you must set your tab width to zero.


            #include<iostre am.h>
            class Vector3D
            {
            public:Vector3D (double a,double b,double c):x(a),y(b),z( c)
            {
            cout<<"\nVector 3D("<<x<<','<<y <<','<<z<<") created...";
            }
            public:const Vector3D&operat or+=(const Vector3D&a)
            {
            x+=a.x;
            y+=a.y;
            z+=a.z;
            return*this;
            }
            friend const Vector3D operator+(const Vector3D&a,cons t Vector3D&b)
            {
            return Vector3D(a.x+b. x,a.y+b.y,a.z+b .z);
            }
            protected:doubl e x,y,z;
            };
            int main()
            {
            Vector3D a=Vector3D(1,2, 3);
            Vector3D b=Vector3D(4,5, 6);
            Vector3D c=Vector3D(0,0, 0);
            c+=a+b;
            return 0;
            }
            ____________
            output:
            Vector3D(1,2,3) created...
            Vector3D(4,5,6) created...
            Vector3D(0,0,0) created...
            Vector3D(5,7,9) created...

            -X



            Comment

            • Guest's Avatar

              #7
              Re: operation overloading headache

              > It means that you must set your tab width to zero.

              Zero tab width???!!!
              You mean this
              ----
              while (1)
              {
              doit();
              }
              ----
              instead of this?
              ----
              while(1)
              {
              doit();
              }
              ----
              Never!!!

              In the code below, why you use inline functions?
              [color=blue]
              > #include<iostre am.h>
              > class Vector3D
              > {
              > public:Vector3D (double a,double b,double c):x(a),y(b),z( c)
              > {
              > cout<<"\nVector 3D("<<x<<','<<y <<','<<z<<") created...";
              > }
              > public:const Vector3D&operat or+=(const Vector3D&a)
              > {
              > x+=a.x;
              > y+=a.y;
              > z+=a.z;
              > return*this;
              > }
              > friend const Vector3D operator+(const Vector3D&a,cons t Vector3D&b)
              > {
              > return Vector3D(a.x+b. x,a.y+b.y,a.z+b .z);
              > }
              > protected:doubl e x,y,z;
              > };
              > int main()
              > {
              > Vector3D a=Vector3D(1,2, 3);
              > Vector3D b=Vector3D(4,5, 6);
              > Vector3D c=Vector3D(0,0, 0);
              > c+=a+b;
              > return 0;
              > }
              > ____________
              > output:
              > Vector3D(1,2,3) created...
              > Vector3D(4,5,6) created...
              > Vector3D(0,0,0) created...
              > Vector3D(5,7,9) created...
              >
              > -X
              >
              >
              >[/color]


              Comment

              • Agent Mulder

                #8
                Re: operation overloading headache

                <- Chameleon ->[color=blue]
                > Zero tab width???!!!
                > You mean this
                > ----
                > while (1)
                > {
                > doit();
                > }
                > ----
                > instead of this?
                > ----
                > while(1)
                > {
                > doit();
                > }
                > ----
                > Never!!![/color]
                </>

                Try

                while(1)doit();

                -X


                Comment

                • Kevin Goodsell

                  #9
                  Re: operation overloading headache

                  Peter van Merkerk wrote:
                  [color=blue]
                  > temporary objects will be
                  > automatically destroyed after the expression which caused them to be created
                  > has been fully evaluated.[/color]

                  After the *full expression* is finished. A full expression is an
                  expression which is not part of a larger expression.

                  -Kevin
                  --
                  My email address is valid, but changes periodically.
                  To contact me please use the address from a recent posting.

                  Comment

                  • Jacek Dziedzic

                    #10
                    Re: operation overloading headache

                    "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in
                    message news:bki9sl$ril $1@nic.grnet.gr ...[color=blue]
                    > [...]
                    > What is indentation? I don't understand the word ;-(
                    > [...][/color]

                    Inserting spaces and tabs into source code so that it's
                    more readable.

                    // code with no indentation
                    for(int k=0;k<3;k++)
                    {std::cout<<k<< std::endl;
                    for(int l=k;l<5;l++)
                    {std::cout<<k*l <<std::endl;};} ;

                    // code with indentation
                    // how exactly you do it is a matter of personal taste,
                    // so others might indent it very differently but I don't care
                    for(int k=0;k<3;k++) {
                    std::cout << k << std::endl;
                    for(int l=k;l<5;l++) {
                    std::cout << k*l << std::endl;
                    };
                    };

                    HTH,
                    - J.


                    Comment

                    Working...