Archived

This topic is now archived and is closed to further replies.

GekkoCube

Vector class vs simple inlined functions?

Recommended Posts

GekkoCube    116
In a header file, why should i implement a Vector class when IK could just as easily declare individual inlined functions, for example: Add(), Delete(), Normalize()...etc.??? If I use individual functions wherever i need them, I wouldnt need to create a Vector object to use the classes methods... what keeps me from doing it this way other than the whole OOP/OOD thing? Also, would it be OK to have the individal inlined functions declared right above my class Vector (as far as programming practice goes?)?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I think it''s just a matter of preference here..
Do you like
Add(a, b)
better than what you can gain with operator overloading:
a + b
?

Or how about
Normalize(a)
compared to
a.Normalize()
?

The latter is better since you take a vector and normalize it (where normalize() is apparently a vector operation), instead of call some context-free normalize-function for a vector.

Share this post


Link to post
Share on other sites
kronq    122
GekkoCube: the way you phrase your decision as ''Vector class'' vs ''individual inlined functions'' sounds like you are under the impression that class methods cannot be inlined; all non-virtual class methods can be inlined just like any flat function. GCC automatically inlines methods that are defined in the class declaration, but I''m not sure about VC or what the C++ standard has to say on that, so you should use the inline keyword to be safe...

So this:

inline Vector normalize( const Vector& v ) { ... }

will compile to exactly the same code as:

class Vector
{
public:
inline Vector normalize() const { ... }
};

Share this post


Link to post
Share on other sites
Xai    1848
please note, you STILL need a class in order to provide you Normalize functions etc ... so that it can tell the difference between normalizing a Vector, vs some other type of object to normalize ...

BUT you can put that function in the global or class scope however you see fit, there is no fundamental difference between the two.

ALSO you can put operator overload function in the global scope as well ... so that if you want to write this

a + b; // when both are vectors

you can implement it as a function in the vector class, OR as a global function ... no difference either way - except the class version can call other private/protected members directly, not needing to go through public functions.

Share this post


Link to post
Share on other sites
GekkoCube    116
kronq,

yes, i was aware of that....but that was besides the point since my question was about utilizing a class versus indivudal inlined functions.

Share this post


Link to post
Share on other sites