Operator Overloading

Started by
12 comments, last by Rob Loach 18 years, 8 months ago
Quote:There really is no need to use an entire class for a vector, since it's such a simple type.
Quote:And yeah(at least from what I have learned), for POD types, you don't in general need operator overloading.
Have to disagree strongly with these two statements. A vector really isn't a POD, and a fully-featured vector class can be quite complex. There are many reasons to use operator overloading, but I'll just give one example. Which of the following would you rather write/read/debug?

1.

v4.x = v1.x + t * (v3.x - v2.x);
v4.y = v1.y + t * (v3.y - v2.y);
v4.z = v1.z + t * (v3.z - v2.z);

2.

v4 = VectorAdd(v1, VectorScale(t, VectorSub(v3, v2)));

3.

v4 = v1 + t * (v3 - v2);
Advertisement
Quote:Original post by jyk
Quote:There really is no need to use an entire class for a vector, since it's such a simple type.
Quote:And yeah(at least from what I have learned), for POD types, you don't in general need operator overloading.
Have to disagree strongly with these two statements. A vector really isn't a POD, and a fully-featured vector class can be quite complex.


Lets just to get one thing straight POD-class types can have member functions therefore can have overloaded operators and overloading operators does not necessarily mean they all have to be member functions most of them can be free-functions.

Granted the definition of POD-class types limits the kind of member functions you can have but that doesn't mean they can't have them at all.
Quote:Original post by jyk
Quote:And yeah(at least from what I have learned), for POD types, you don't in general need operator overloading.


Have to disagree strongly with these two statements. A vector really isn't a POD, and a fully-featured vector class can be quite complex. There are many reasons to use operator overloading, but I'll just give one example. Which of the following would you rather write/read/debug?



Never said anything about vectors being POD or not. :) For the record, no I dont think vectors are POD type.

Also, I wasn't talking about a POD-class (I was referring strictly POD-type), which would change the situation and agree with snk_kid on that topic.
It seems more practical when making classes that require more complex mathematical functions. Take, for example, fractions. If you are your making a fraction class, you'd almost definately want to overload the addition operator to have the fraction add correctly.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement