Archived

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

ogracian

Could be possible to use operators in C like in C++?

Recommended Posts

Hello I am writing some vector math operations like add, dot, scale, mag, etc.. using C (NOT C++), but my trouble is that I NEED to use C, but I am wondering if is possible to use Operator overload functions like in C++ but using C? PS: My vector definition is just a single struct as follows: typedef struct { float x; float y; float z; } VECTOR; EXAMPLE: VECTOR A, B, C; A = B + C; // Is this possible in C?

Share this post


Link to post
Share on other sites
Simply, no it''s not.

Operator Overloads were only added to C++, and they go against a lot of what C is all about (C is not object oriented)

You have to do something like:

  
void vector_add( VECTOR lhs, VECTOR rhs, VECTOR *answer )
{
answer->x = lhs.x + rhs.x;
answer->y = lhs.y + rhs.y;
answer->z = lhs.z + rhs.z;
}

Then:
  
VECTOR A, B, C;

vector_add( B, C, A );



War Worlds - A 3D Real-Time Strategy game in development.

Share this post


Link to post
Share on other sites
Operator overloading is just syntactic sugar anyway - and much like sugar, it can be bad for you, especially if you overload them to mean something that the operator didn''t originally mean.

Nothing wrong with expicitly calling a function called vector_add ... people will know what you mean.

--


Get a stripper on your desktop!

Share this post


Link to post
Share on other sites