Vector class vs simple inlined functions?

Started by
4 comments, last by GekkoCube 21 years, 1 month ago
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?)?
Advertisement
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.
ok thanks.

i''ll just have both methods in one file,...this way ill use whatever i feel like is best for a situation.

thank you.
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 { ... }
};
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.
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.

This topic is closed to new replies.

Advertisement