c++: operator overloading
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]
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
Here''s a tutorial on operator overloading you might want to read
i don''t get this from the tutorial you gave me:
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
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
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..
hope that wasn''t really your question..
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.
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.
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?
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?
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."
-----------
VenDrake
"My stupid jar is full. I can''t talk to you anymore."
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]
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;}
// 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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement