Operator overloading (less commonly known as ad-hoc polymorphism) is a specific case of polymorphism (part of the OO nature of the language) in which some or all operators like +, = or == are treated as polymorphic functions and as such have different behaviours depending on the types of its arguments. Operator overloading is usually only syntactic sugar. It can easily be emulated using function calls.
In computer science, syntactic sugar in a programming language is syntax designed to make things easier to read or to express, while alternative ways of expressing them exist. It makes the language "sweeter" for humans to use: things can be expressed more clearly, more concisely, or in an alternative style that some may prefer.
That means you can live without it
Operator overloading exists in C++/OOP and if you use C or use C++ without OOP, operator overloading isn't possible. If you use C++/OOP, it is very useful and recommended to use operator overloading. Under [3] of
C++ Programming Practice Guidelines you will find that it is recommended to use this more often.
I'll give you a simple example.
Code: Select all
class Coefficient
{
public:
Coefficient(double initVal)
{ myValue = initVal; myAccesses = 0; }
double GetValue(void) const
{ myAccesses++; return myValue; }
bool SetValue(double v)
{
myAccesses++;
if (v<0 || v>1) { return false; }
myValue = v;
return true;
}
private:
double myValue;
mutable int myAccesses;
};
Code: Select all
Coefficient c (0.5);
// AND LATER...
c.SetValue(0.75);
If you think you need to use something like c = 0.75; to be used instead of
c.SetValue(0.75); you can do it as follows.
Code: Select all
class Coefficient
{
public:
Coefficient(double initVal)
{ myValue = initVal; myAccesses = 0; }
double GetValue(void) const
{ myAccesses++; return myValue; }
bool operator= (double v)
{
myAccesses++;
if (v<0 || v>1) { return false; }
myValue = v;
return true;
}
private:
double myValue;
mutable int myAccesses;
};
Code: Select all
Coefficient c (0.5);
// AND LATER...
c = 0.75; // ACTUALLY CALLS: c.operator=(0.75);
This is just a simple use. You can find lots of information about C++ operator overloading in the following article.
http://www.csse.monash.edu.au/~jonmc/CS ... /text.html
If you are going to use this on DSPs (I know you are an embedded guy

), I don't think C++/OOP is a good option as it would be really inefficient on embedded platforms.