hi,
I am not able to understand, what is the need of operator overloading in c++ and what is its benefit?
Thanks
operator overloading in c++
-
- Lieutenant
- Posts: 92
- Joined: Tue Oct 27, 2009 7:50 pm
Re: operator overloading in c++
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.
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.
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.
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.
That means you can live without itIn 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.
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);
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);
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.
-
- Lieutenant
- Posts: 92
- Joined: Tue Oct 27, 2009 7:50 pm
Re: operator overloading in c++
Thanks Neo.
Re: operator overloading in c++
Operator Overloading means operator use twice in a row. Operators such as +,- ,etc. Now i will explain with example.
int i=10;
i++; // This is called operator overloading.
cout<<10;
int i=10;
i++; // This is called operator overloading.
cout<<10;