Operator overloading in C

user142019 picture user142019 · Aug 5, 2010 · Viewed 50.6k times · Source

I am trying to overload some operators:

/* Typedef is required for operators */
typedef int Colour;

/* Operators */
Colour operator+(Colour colour1, Colour colour2);
Colour operator-(Colour colour1, Colour colour2);
Colour operator*(Colour colour1, Colour colour2);
Colour operator/(Colour colour1, Colour colour2);

I get this error for each tried overloading:

expected '=', ',', ';', 'asm' or '__attribute__' before '+' token

I can't find any good documentation on operator overloading. Googling results in C++ tutorials which use classes. In C there are no classes. Can anyone help me? Thanks.

Answer

Jerry Coffin picture Jerry Coffin · Aug 5, 2010

C does not support operator overloading (beyond what it built into the language).