Operator Overloading

Started by
12 comments, last by Rob Loach 18 years, 9 months ago
Hi, im currently learning operator overloading, and it does seem useful at times, but how practical is it? Is operator overloading something that I shoudl get a full grasp on?
Advertisement
Absolutely!
Consider this bit of code
class CVector{public:    CVector &operator=(const CVector &v);private:    float point[3];};CVector &CVector::operator=(const CVector &v){    if(&v != this)    {        point[0] = v.point[0];        point[1] = v.point[1];        point[2] = v.point[2];    }    return *this;}


Without the = operator, you wouldn't be able to do this:
CVector vec1, vec2;vec1 = vec2;


...and that's just a small piece of what operator overloading can do for you, in it's simplest form =)
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
Quote:Original post by ematsui
Hi, im currently learning operator overloading, and it does seem useful at times, but how practical is it?

Is operator overloading something that I shoudl get a full grasp on?


It becomes more than a mere convinience once you get into templates, so yes, it will help to learn.
Quote:Original post by Android_s
Absolutely!
Consider this bit of code
*** Source Snippet Removed ***

Without the = operator, you wouldn't be able to do this:
CVector vec1, vec2;vec1 = vec2;


...and that's just a small piece of what operator overloading can do for you, in it's simplest form =)

Actually, that can be done automatically:
struct vector {  int x, y, z;};...struct vector v1 = {10, 11, 12};struct vector v2;v2 = v1;

There really is no need to use an entire class for a vector, since it's such a simple type.
Quote:
There really is no need to use an entire class for a vector, since it's such a simple type.

On the contrary, if you put a lot of stuff into your vector class, for example cross-product, dot-product, length calculations etc it's quite handy =)
My posted vector class was extremely simple, just to show one practical use of operator overloading.
----------------------------------------------------------------------------------------------------------------------"Ask not what humanity can do for you, ask what you can do for humanity." - By: Richard D. Colbert Jr.
Quote:Original post by Android_s
Quote:
There really is no need to use an entire class for a vector, since it's such a simple type.

On the contrary, if you put a lot of stuff into your vector class, for example cross-product, dot-product, length calculations etc it's quite handy =)
My posted vector class was extremely simple, just to show one practical use of operator overloading.

How does that make it more handy than simply including the functions in the same file?
Quote:Original post by bytecoder
There really is no need to use an entire class for a vector, since it's such a simple type.


I think he was just using it as an example of how operator overloading works with classes, regardless If it was a vector class or not.

And yeah(at least from what I have learned), for POD types, you don't in general need operator overloading.

Quote:Original post by radioact1ve
Quote:Original post by bytecoder
There really is no need to use an entire class for a vector, since it's such a simple type.


I think he was just using it as an example of how operator overloading works with classes, regardless If it was a vector class or not.

And yeah(at least from what I have learned), for POD types, you don't in general need operator overloading.

I know, I was just pointing out that there's an easier way for his particular example :)
Quote:Original post by bytecoder
Quote:Original post by Android_s
Without the = operator, you wouldn't be able to do this:
CVector vec1, vec2;vec1 = vec2; 

Actually, that can be done automatically by just using a POD type (at least in C)

People tend to forget that = works on structs and classes, even in C. I've also seen people write copy constructors that do exactly what the default copy constructors do.
Quote:Original post by bytecoder
There really is no need to use an entire class for a vector, since it's such a simple type.

That statement is wrong for so many reasons.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by ematsui
Hi, im currently learning operator overloading, and it does seem useful at times, but how practical is it?

Is operator overloading something that I shoudl get a full grasp on?


It is frequently necessary to overload the = operator, so yes, everyone should understand operator overloading.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement