Jump to content
  • Advertisement

Archived

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

3dburns

c++: operator overloading

This topic is 5324 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!