operation overloading headache

G

Guest

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+=(const Vector3D &a);
friend const Vector3D operator+(const Vector3D &a, const Vector3D &b);
};

const Vector3D &Vector3D::eek:perator+=(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;
}
 
P

Peter van Merkerk

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?

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.
 
A

Alf P. Steinbach

The code below I suspect creates memory leaks:

Nope.

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


In the main() in line "c += a + b;" when compiler runs "a + b" creates a new object.

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'.


After "c += 'new_object'"
we have an unreferenced object which created from "a + b" and not destroyed.
am I right?
No.


What I can do to avoid this?

Write correct code.


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'.

public:
Vector3D(float x, float y, float z) : _x(x), _y(y), _z(z) {}

Do use indentation, and also some blank lines.

const Vector3D &operator+=(const Vector3D &a);


friend const Vector3D operator+(const Vector3D &a, const Vector3D &b);
};

const Vector3D &Vector3D::eek:perator+=(const Vector3D &a) {
_x += a._x; _y += a._y; _z += a._z; return *this;
}

OK except for formatting and names.

const Vector3D operator+(const Vector3D &a, const Vector3D &b) {
return Vector3D(a._x + b._x, a._y + b._y, a._z + b._z);
}

OK except for formatting and names.


void main() {

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

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


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

Guest

The code below I suspect creates memory leaks:
Nope.
But it's prone to have other bugs since you don't use indentation.

Thanks for your response.
What is indentation? I don't understand the word ;-(
Write correct code.

lol ;-)
Do use indentation, and also some blank lines.
OK except for formatting and names.

I "pack" the code to avoid a big message.
To be honest, Vector3D looks like someone else's code reformatted.

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
 
A

Agent Mulder

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

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<iostream.h>
class Vector3D
{
public:Vector3D(double a,double b,double c):x(a),y(b),z(c)
{
cout<<"\nVector3D("<<x<<','<<y<<','<<z<<") created...";
}
public:const Vector3D&operator+=(const Vector3D&a)
{
x+=a.x;
y+=a.y;
z+=a.z;
return*this;
}
friend const Vector3D operator+(const Vector3D&a,const Vector3D&b)
{
return Vector3D(a.x+b.x,a.y+b.y,a.z+b.z);
}
protected:double 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
 
G

Guest

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

Zero tab width???!!!
You mean this
 
K

Kevin Goodsell

Peter said:
temporary objects will be
automatically destroyed after the expression which caused them to be created
has been fully evaluated.

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

-Kevin
 
J

Jacek Dziedzic

[...]
What is indentation? I don't understand the word ;-(
[...]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top