Jump to content
  • Advertisement


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


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

This topic is 6278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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;


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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!