Jump to content
  • Advertisement
Sign in to follow this  
PennstateLion

Operator Overloading

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


Link to 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


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to 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


Link to 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


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!