Critique my 3D Vector Class

Started by
11 comments, last by etothex 17 years, 10 months ago
Quote:Original post by Ravyne
Quote:Original post by JohnBolton
These functions should return a reference to *this, not void.
	inline void		operator =(const Vec3f &rhs);    ...

This is actually a concious decision on my part for the sake of optimization. The returned reference is rarely used and, although technically 'correct' (or at least adhering to common practice,) promotes ugly code like a = b += c + d. So, to my mind, I've gained some optimization and prevented ugly code, and the fact that a direct reference to the variable in the next statement should be no less efficient seals the deal. Admittedly, it does go against the norm though.

hmmm... Returning void instead of a reference to Vec3f will not result in meaningful optimization (if any at all), and you are not preventing ugly code, you are reducing the usability of your class. You don't like using the result of an assignment, but others might find it valuable. Code using the result of assignment is very common (something like this perhaps):
    while ( Vec3f::rad( v = GetVector() ) > 0 )    {        // use v ...    } 
And since returning a reference to *this is the convention, your class would be considered broken by those that might depend on it.

Just my opinion, so disregard if you like.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Advertisement
Quote:Original post by Ravyne
This is actually a concious decision on my part for the sake of optimization. [...] Admittedly, it does go against the norm though.
What incredibly naive and ignorant logic. First, it isn't an optimization. If the returned reference is not used, it will never be returned by the function. The optimizer is more than capable of handling such trivial cases. Second, going against the norm should generally be considered a grievous offense, unless there is a compelling reason. Since you lack a compelling reason here, you need to revise your functions to behave the way they're supposed to.

Oh, and because most people tend to screw this up: Implement + in terms of +=, not the other way around. The correct way to write operator+ is:
return Vec3f( *lhs ) += rhs;
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
I would worry about the compiler (or the programmer) if a + or += operation wasn't inlined. And if it's inlined, the return reference is easily discarded.

This topic is closed to new replies.

Advertisement