Hi,

Can any body explain me about overloading concept of () operator?
I need to know why do we need to overlload () operator and how do we achieve that?

Thanks in Advance

() isn't a operator. Do you mean function-overloading?

int foo(int bar)
{
    return bar*2;
}

float foo(float bar)
{
    return bar*2.0f;
}

In the above example, I've created two functions with the same name and functionality. But one is for ints and one is for floats => function overloading

I just need to know why we need to overload () it?
If u have any document/link then tell me.

If you are asking "why is overloading functions useful", then the answer is because sometimes we want two or more functions with the same name which provide the same functionality, but which require different parameters.

Lets take an actual example and look at the append() function on std::string. Here are a few of the overloaded functions:

string& append ( const string& str );
string& append ( const char* s );
string& append ( const char* s, size_t n );

All three of these functions have the same name and do basically the same thing - they append something to a string. So why do we need more than one version? Well assume you want to append another string to your string, then you would call the first version which takes the string you want to add on as a parameter. But wait, what if you have an array of char instead of a string? Well, just use the second version of the function which takes a pointer to char. But what if you just wanted to append part of the char* string? Then use the 3rd version which takes the char* parameter as well as the number of characters that should be appended.

google "c++ functor"
Mostly used for complex predicates of STL algorithms i.e. remove_if()

When you overload the () operator, it is called the conversion operator. It provides explicit instructions as to how to implicitly convert a class object into a built in plain old data type. At least that's how my reference explains it.

>At least that's how my reference explains it.
You might be misunderstanding your reference. There's an implicit conversion operator and a function call operator The function call operator is a general member function in terms of return value and parameters. The implicit conversion operator takes no parameters ( *this is implied) and the return value of of the type the operator defines. Non-explicit copy constructors and assignment operators give you an implicit conversion in the opposite direction (from whatever type to the type of the class):

#include <iostream>

class foo {
  double base;
public:
  // Implicit copy constructor (type T -> new type foo)
  foo ( double d )
    : base ( d )
  {}

  // Assignment operator (type T -> existing type foo)
  foo& operator= ( double d )
  {
    base = d;
    return *this;
  }

  // Function call operator (does whatever you might want)
  std::ostream& operator() ( const char *s )
  {
    return std::cout<< s <<'\n';
  }

  // Implicit conversion operator (existing type foo -> type T)
  operator double()
  {
    return base;
  }
};

int main()
{
  foo f = 123.456;

  std::cout<< std::boolalpha << !!f ( "This is a test ") <<'\n';
  std::cout<< f <<'\n';
  f = 987.654;
  std::cout<< f <<'\n';
}

Basically the function call operator gives you the option of making an object act like a function. Hence, the "functor" or function object that jencas mentioned.

>() isn't a operator.
Yes, it is. [] is an operator too, and , is an operator as well, sometimes to the surprise of even experienced C++ programmers.

Actually my reference doesn't indicate specifically that the conversion operator is an overloaded version of the () operator. In discussing the conversion operator, however, it indicates that "ALL (the reference's emphasis, not mine) operator overloading works the same---you declare an overloaded operator using the keyword operator" which is consistent with the example provided:

class Counter
{
  public:
    operator unsigned short()
    {
       return (int(itsval));
    }
  private:
    int itsVal;
};

It doesn't really state whether the operator is () or, in this case, whether the operator is unsigned int (). Since () is an operator and unsigned int is a type, I assumed, perhaps incorrectly, that the conversion operator is an overloaded version of the () operator.

So is the conversion operator not considered overloading the () operator whereas the function call operator is considered overloading the () operator, both are considered overloading the () operator, or neither are considered overloading the () operator?

>So is the conversion operator not considered overloading the () operator whereas the
>function call operator is considered overloading the () operator, both are considered
>overloading the () operator, or neither are considered overloading the () operator?
The function call operator and operator() are the same thing. Here's how you can break it down, and it depends on what goes after the operator keyword:

T operator() ( ... )
{
  ...
}

This is overloading the () operator because the operator you want to overload follows the operator keyword (operator()). Notice the seemingly redundant parameter lists, which is a good sign that the () operator is being overloaded.

operator T()
{
  ...
}

This is overloading the implicit conversion operator to type T because the type T follows the operator keyword (operator T). A space is required between the two so that the construct isn't treated as an identifier. The () in that is the parameter list, not the operator to overload.

Your book is correct, but it sounds like it's trying to be too clever in the explanation and causing confusion.

Comments
Wow I honestly was unaware of the conversion overload O_O - whoo! Thank you! =)

So the conversion operator is an operator without a symbol (that is no & or || or << or []) to represent it. Ha.

Thanks for the education!

Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:

It allows you to provide an intuitive interface to users of your class, plus makes it possible for templates to work equally well with classes and built-in/intrinsic types.
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function calls:

class Ramu {
 public:
   ...
 };
 #if 0
   // Without operator overloading:
   Ramu add(const Ramu& x, const Ramu& y);
   Ramu mul(const Ramu& x, const Ramu& y);
   Ramu f(const Ramu& a, const Ramu& b, const Ramu& c)
   {
     return add(add(mul(a,b), mul(b,c)), mul(c,a));    // Yuk...
   }
 #else
   // With operator overloading:
   Ramu operator+ (const Ramu& x, const Ramu& y);
   Ramu operator* (const Ramu& x, const Ramu& y);
 
   Ramu f(const Ramu& a, const Ramu& b, const Ramu& c)
   {
     return a*b + b*c + c*a;
   }
 #endif

------Benefits:------------
By overloading standard operators on a class, you can exploit the intuition of the users of that class. This lets users program in the language of the problem domain rather than in the language of the machine.
The ultimate goal is to reduce both the learning curve and the defect rate.
---------Examples:---------
Here are a few of the many examples of operator overloading:
• myString + yourString might concatenate two std::string objects
• myDate++ might increment a Date object
• a * b might multiply two Number objects
• a might access an element of an Array object
• x = *p might dereference a "smart pointer" that "points" to a disk record — it could seek to the location on disk where p "points" and return the appropriate record into x

Wish You Good luck...

Regards,
Rammohan Alampally,
<snip false signature>

>So the conversion operator is an operator without a symbol (that is no & or || or << or []) to represent it.
The conversion operator is an operator with a type to represent it, the type being the symbol.

>Operator overloading allows C/C++ operators to have
>user-defined meanings on user-defined types (classes).
Nit: C/C++ only makes sense when the feature/technique/convention is available both to C and C++. Operator overloading is not, nor are classes as defined by C++. Thus, when you say C/C++, you mean C++ and should say C++ to avoid furthering myths about the relationship between C and C++.

This article has been dead for over six months. Start a new discussion instead.