Jump to content
• ### What is your GameDev Story?

• Advertisement

Public Group

# Operator Overloading

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

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 this post

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

#### Share this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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 this post

##### 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 this post

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

##### 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 this post

##### 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 is frequently necessary to overload the = operator, so yes, everyone should understand operator overloading.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

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

(You must login to your GameDev.net account.)

• ### Popular Now

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

• Total Topics
634101
• Total Posts
3015529
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!