operator overloading in c++

C, C++, Visual C++, C++.Net Topics
Post Reply
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

operator overloading in c++

Post by SukhdeepMankoo » Mon May 10, 2010 2:04 pm

hi,
I am not able to understand, what is the need of operator overloading in c++ and what is its benefit?
Thanks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: operator overloading in c++

Post by Neo » Mon May 10, 2010 7:33 pm

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.
SukhdeepMankoo
Lieutenant
Lieutenant
Posts: 92
Joined: Tue Oct 27, 2009 7:50 pm

Re: operator overloading in c++

Post by SukhdeepMankoo » Wed May 12, 2010 11:19 am

Thanks Neo.
User avatar
smithdwsn
Posts: 2
Joined: Wed May 12, 2010 11:42 pm

Re: operator overloading in c++

Post by smithdwsn » Thu May 13, 2010 8:40 pm

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;
Post Reply

Return to “C/C++ Programming”