silly question on Operator Overloading

Started by
3 comments, last by JohnBolton 18 years, 10 months ago
i been stuck on Operator Overloading mainly because i really dont understand its concept :*) can someone explain to me what exactly the difference in overloading an operator? for example the + it be easier to learn it if i knew and understood but,after reading almost a half chapter of it,i am still clueless as to what exactly its doing :*) told you it was a silly question,somebody help me please :(
Advertisement
So what exactly are you confused about?

The + operator is a simple example to work with. C++ defines the + operator for all built-in types for which it makes sense; you can add ints, floats, doubles, etc., as you would expect.

The classic example of operator overloading is for a vector class. Let's say you have a 3d vector class, like this:

class Vector
{
public: float x, y, z;
};

You may already know that you add vectors by adding their components. Without operator overloading, every time you wanted to add two vectors, you would have to write:

v.x = v1.x+v2.x;
v.y = v1.y+v2.y;
v.z = v1.z+v2.z;

Hard to type, hard to read, and introduces the possibility of error. It would be much better if you could just write:

v = v1+v2;

And by overloading the + operator for class Vector, you can.

Once you understand the concept, it's just a matter of learning the syntax, which your book should be able to help you with.

[Edit: Actually there are ways to avoid writing it out every time without using operator overloading, but if you're using C++ you might as well use the tools it provides, as they have some advantages over the other methods.]
In short, overloading an operator allows you to use those operators on your own data types. Say for instance, you have a class Grid:

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


And you have a Grid now:

Grid myGrid;myGrid.x = 3;myGrid.y = 2;


Overloading the ++ operator can allow you to do a number of things.

Grid Grid::operator++(){  x++;  y++;  Grid temp;  temp.x = this->x;  temp.y = this->y;  return Grid;}


So now you can do this:

Grid = Grid++;


And now the values which were 3,2 are now 4,3.

If you still don't understand, feel free to come back and ask.
yeah,i am still confused about it :(

maybe i should go back and review Classes and Pointers,i sort of understand it but i still fail to see where the operator ++ changes or what its doing :(

but thanks you two for trying,it should be easy but i just cant figure it out



....

so this whole thing:
Grid Grid::operator++()
{
x++;
y++;
Grid temp;
temp.x = this->x;
temp.y = this->y;
return Grid;
}

is only to make it easier by typing this:
Grid = Grid++;

is this what Operator overloading means? :*)



Operators in C++ are implemented using functions. For example, the name of the function for the '+' operator is "operator+". When you overload an operator, you are overloading its function. "Overloading a function" means to provide another function with same name but different parameters.

Now, classes don't have most operators automatically created for them, so if you want an operator for a class, you have to write it yourself. A good example of where creating operators is handy is a 3D vector class.

If you want to add two vectors, you could write this function.
    vector3 Add( vector3 const & a, vector3 const & b )    {        vector3 result;        result.x = a.x + b.x;        result.y = a.y + b.y;        result.z = a.z + b.z;                return result;    }    ...    vector3 d,e,f;    d = Add( e, f ); 
Operations on vectors very closely match standard arithmetic operations, so you might think it would be handy to use the '+' operator to add vectors, instead of explicitly calling a function. In the end, there is no difference, but it might make your code easier to read. Here is how you would implement the '+' operator for a 3D vector:
    vector3 operator+( vector3 const & a, vector3 const & b )    {        vector3 result;        result.x = a.x + b.x;        result.y = a.y + b.y;        result.z = a.z + b.z;                return result;    }    ...    vector3 d,e,f;    d = e + f; 
Internally, the compiler converts the line "d = e + f" to
    d = operator+( e, f ); 
which BTW is also valid code.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement