Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator Overloading in C

In C++, I can change the operator on a specific class by doing something like this:

MyClass::operator==/*Or some other operator such as =, >, etc.*/(Const MyClass rhs) {
    /* Do Stuff*/;
}

But with there being no classes (built in by default) in C. So, how could I do operator overloading for just general functions?

For example, if I remember correctly, importing stdlib.h gives you the -> operator, which is just syntactic sugar for (*strcut_name).struct_element.

So how can I do this in C?

Thank you.

like image 848
Leif Andersen Avatar asked Apr 10 '10 03:04

Leif Andersen


Video Answer


2 Answers

Plain old C does not have operator overloading in any form. The -> "operator" to access a member of a pointer is standard C and is not introduced by any header file.

like image 119
Mark Rushakoff Avatar answered Oct 05 '22 04:10

Mark Rushakoff


Built-in operators in C language are overloaded. The fact that you can use binary + to sum integers, floating-point numbers and perform pointer arithmetic is a canonical example of operator overloading.

However, C offers no features for user-level operator overloading. You can't define your own operators in C.

like image 45
AnT Avatar answered Oct 05 '22 03:10

AnT