Page 1 of 1

operator overloading in c++

Posted: Mon May 10, 2010 2:04 pm
by SukhdeepMankoo
hi,
I am not able to understand, what is the need of operator overloading in c++ and what is its benefit?
Thanks

Re: operator overloading in c++

Posted: Mon May 10, 2010 7:33 pm
by Neo
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 :D

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.

Re: operator overloading in c++

Posted: Wed May 12, 2010 11:19 am
by SukhdeepMankoo
Thanks Neo.

Re: operator overloading in c++

Posted: Thu May 13, 2010 8:40 pm
by smithdwsn
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;