• Advertisement

Archived

This topic is now archived and is closed to further replies.

c++: operator overloading

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Real quick, can someone give me an example of how operator overloading works?? If you can show me using a string example that would be easier for me to understand thx. [edited by - 3dburns on January 14, 2004 5:45:35 PM]

Share this post


Link to post
Share on other sites
Advertisement
All operator overloading is redefining the way the build in operators work respective to your built in types - there''s no restriction in what an overloaded operator can do, you can redefine operator+ to actually perform a subtraction for example but you should stick to mimicing the way the built in operators work to avoid confusion and bugs

Here''s a tutorial on operator overloading you might want to read

Share this post


Link to post
Share on other sites
i don''t get this from the tutorial you gave me:


CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}


I understand that it''s a function but i don''t get the:

temp.x = x + param.x;
...etc

why do you use something.something

Share this post


Link to post
Share on other sites
why do you use the dot operator? That''s the way you refer to an item inside a class/struct.

hope that wasn''t really your question..

Share this post


Link to post
Share on other sites
Since the CVector class seems to be primarily the members float x, y (haven''t read the tutorial), the overloaded operator is adding the individual elements (since there is no pre-defined CVector + CVector operator, while a float + float operator does exist.)

That said, you''d be better off with return CVector( this->x + param.x, this->y + param.y ); The compiler can optimize this better, and although it makes seemingly little difference, the difference will add up when your engine gets more advanced.

Share this post


Link to post
Share on other sites
Umm, I have a question. ''temp'' is created on the stack, so it''s deleted when operator+ ends. So how can you return ''temp''? Isn''t it dead by the time the calling function gets it?

Or does it work because it is OK to use the object even after the destructor has been called?

Or maybe, does return actually pass a copy?

Share this post


Link to post
Share on other sites
Returning temp creates a copy. It''s not a problem. Returning &temp, now that would be a problem.


-----------
VenDrake

"My stupid jar is full. I can''t talk to you anymore."

Share this post


Link to post
Share on other sites
quote:
Original post by pinacolada
Umm, I have a question. 'temp' is created on the stack, so it's deleted when operator+ ends. So how can you return 'temp'? Isn't it dead by the time the calling function gets it?

Or does it work because it is OK to use the object even after the destructor has been called?

Or maybe, does return actually pass a copy?


Its perfectly fine since the return is not by reference. 'temp' will create a new temporary variable for assignment or initialization of another when exiting the function just like any other function would do.

EDIT: And like the above poster pointed out a return by reference is not being made which is key here.

[edited by - nervo on January 14, 2004 8:13:16 PM]

Share this post


Link to post
Share on other sites
Can someone go through and explain to me what each line does? For some reason I get really confused on this concept.


// vectors: overloading operators example

#include <iostream.h>

class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};

CVector::CVector (int a, int b) {
x = a;
y = b;
}

CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}

int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}

Share this post


Link to post
Share on other sites

// vectors: overloading operators example

#include <iostream.h>

class CVector {
public:
int x,y; //public variable declarations

CVector () {}; //default constructor must be explicitly defined because other constructors exist

CVector (int,int); //constuct a new CVector object by passing two ints to it like Cvector "x(5,10);"

CVector operator + (CVector); //overload the addition operator to give it a user defined meaning

};

CVector::CVector (int a, int b) { /* the constructor body is defined here by assigning to x and y (variables that are part
of the class) the user supplied ints when constructed. Remember that the compiler only executes a constructor. */

x = a;
y = b;
}

CVector CVector::operator+ (CVector param) {
CVector temp; /*create an object of the same class within a member function that will have local scope to the function.
Again the compiler here will invoke this section of code.
The '+' operator is a binary operator and its left operand is
implicit here because the user defined operator function is
part of the class. (that is why the parameter list has only one
parameter for the right-side operand) */

temp.x = x + param.x; /*assign to member variable x of object temp the value of member variable x of object param */
temp.y = y + param.y; /*same deal here with y variable*/
return (temp); /* temp obect is returned from the function. A new temporary object is created in memory because temp here
will be popped off the stack since its scope is local to the
function here. when temp is returned it may be used in
assignment to another variable or may be part of another '+'
operation with a different variable.*/

}

int main () {
CVector a (3,1); //use constructor CVector::CVector(int,int)

CVector b (1,2); //same here

CVector c; //default constructor CVector::CVector() invoked

c = a + b; /* addition operation here. What is happening
here is that the x and y components of both a and b are being added together with the temp object and when returned the
compiler will synthesize an assignment operation to make c a copy of temp*/

cout << c.x << "," << c.y;
return 0;
}


After writing halfway through this It dawned on me that you were asking what is "something.something". That is a bad sign if you are trying to build classes, because that is something you should have learned much before touching on this subject. But since I already started writing I continued on with it just in case you could get anything out of it. Truthfully, there are alot of little intricacies in building classes that I didn't even go into, but a good book should show you the way. Good luck.

[edited by - nervo on January 15, 2004 1:17:52 AM]

Share this post


Link to post
Share on other sites
I know that "something.somthing" is how you call a function that is inside the class but for some reason operator overloading is going right over my head right now. Thanks for the help.

Share this post


Link to post
Share on other sites
I think what you need to understand is this.... when you write:

z=x+y

the compiler does:

operator=(z,operator+(x,y)) (or something along those lines)

So you are overloading (ie providing versions of) operator{whatever} functions, in the same way as any other function would be overloaded. In this case, it is important to realise what the return type must be.

For example, operator+ should return by value... since adding conventionally creates a temporary... but operator= should return a reference (so you can do x=y=z=0)

Hope that helps clarify a little more.

Share this post


Link to post
Share on other sites
Ok, first off, there is an error in the tutoral. In order for that tutural to work, you must NOT have dynamic memory allocated in the class. Dynamic memory referse to arrays/objects that are created using 'new.' To make your Vector class robust, you need to add what is called a copy constructor. Basicly, a copy constructer takes a refrenece to another instance of its class.

//////////////////////////////////////////////////

CVector::CVector(const CVector &v) : x(v.x), y(v.y)
{
}

//////////////////////////////////////////////////

You may not have seen a single colon after a constructor. The single colon is lets the programmers assin values to member variables without entering the body of the constructor. The syntax is : varable(value), nextv(value) and so on. The reason you need to add a copy constructor is so the compiler can make temperary copies of items to be placed on the stack. For example, when you return a CVector, the copy constructor is called to place a *copy* onto the stack. If you do not specify a copy constructor, the compiler may or MAY NOT copy the data correctly.

Ok, now to get to your question. There is one small problem I see with this code. The variable "param" should be a reference (i.e &). As for your question about "something.somthing" - the . in C++ means that you'd like a certain variable out of that struct/class. So "param.x" gets the value of the variable x in that instance of param.

//////////////////////////////////////////////////

CVector CVector::operator+ (CVector ¶m)
{
// creates a new vector to store results in
CVector temp;

// add this vector with param
temp.x = x + param.x;
temp.y = y + param.y;

// here's where the copy constructor comes into play
// a copy of temp is placed on the stack, and the origional temp
// is destroyed, which is ok because the stack has a copy of it.
return (temp);
}

//////////////////////////////////////////////////


[edited by - Evangelion on January 15, 2004 3:18:05 PM]

Share this post


Link to post
Share on other sites
Yah, heh, I just got to the Operator Overloading chapter in my book (chapter 13, I do believe). Anyway, I would answer your question but I''m not quite done of the chapter yet.


Share this post


Link to post
Share on other sites
What book do you have?? Who''s it by and has it been a good book to learn with so far??

Share this post


Link to post
Share on other sites

  • Advertisement