• ### What is your GameDev Story?

Public Group

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

## Recommended Posts

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?

##### Share on other sites
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 =)

##### Share on other sites
Quote:
 Original post by ematsuiHi, 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.

##### Share on other sites
Quote:
 Original post by Android_sAbsolutely!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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by bytecoderThere 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.

##### Share on other sites
Quote:
Quote:
 Original post by bytecoderThere 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 :)

##### Share on other sites
Quote:
Original post by bytecoder
Quote:
 Original post by Android_sWithout 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 bytecoderThere 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.

##### Share on other sites
Quote:
 Original post by ematsuiHi, 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?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 27
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634101
• Total Posts
3015529
×