473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

const Vector3D &Vector3D::operator+=(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;
}
Jul 19 '05 #1
9 2182
Not OperatION, operatOR. Sorry!
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::operator+=(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;
}

Jul 19 '05 #2
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bk**********@nic.grnet.gr...
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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


Jul 19 '05 #3
On Sat, 20 Sep 2003 21:06:41 +0300, "<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote:
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.

------------------------------
class Vector3D {
protected:
float _x, _y, _z;
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::operator+=(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.

Jul 19 '05 #4
> >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 ;-(
What I can do to avoid this?


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


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
Jul 19 '05 #5
<Alf P. Steinbach>
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

Jul 19 '05 #6
> 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?
#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

Jul 19 '05 #7
<- Chameleon ->
Zero tab width???!!!
You mean this
----
while (1)
{
doit();
}
----
instead of this?
----
while(1)
{
doit();
}
----
Never!!!

</>

Try

while(1)doit();

-X
Jul 19 '05 #8
Peter van Merkerk wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #9
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in
message news:bk**********@nic.grnet.gr...
[...]
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.
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
0
by: Plumer | last post by:
Hello everyone, Yesterday I posted a message about implementing drag & drop in a TreeView control. I'm having real difficulty getting this to work -- the process seems to be incredibly...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
4
by: Ole Nielsby | last post by:
I'm puzzed by this: /***code begin***/ class X {}; class Y : public X {}; class A { public: virtual void m(X x) {std::wcout << L"A\n";} }; class B : public A {
7
by: jaegertw2 | last post by:
Hi guys, I've seen a few other similar posts, but i still cant figure out exactly what I'm doing wrong or what i need to do to fix it. I'm trying to find the ID number of the last record in a...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
16
by: foolsmart2005 | last post by:
ComplexNum ComplexNum::operator +(const ComplexNum& rhs) const // - operation is the similar way { ComplexNum ans; ans.real = real + rhs.real; ans.imaginary = imaginary + rhs.imaginary;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.