0

Here I see some clear examples of operator overloading. The syntax is as following:

type operator sign (parameters) { /*...*/ }

In the code that I work on I found the following string:

bool operator () (int a)

The first problem that I have with this code is there is not operator sign (nothing like operator+ or operator*). The second problem is that in front of the arguments (int a) I see () and I cannot find out what it is supposed to do. Could you please help me with that?

YXD
  • 31,741
  • 15
  • 75
  • 115
Roman
  • 124,451
  • 167
  • 349
  • 456
  • 1
    You just cannot ask about every single [syntax of C++ that differs from Java](http://stackoverflow.com/questions/15092844/what-can-stay-between-struct-and-except-the-structure-name). Please read one or more item from [the C++ book list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Bo Persson Feb 26 '13 at 17:23

3 Answers3

7

You're incorrect about there being no operator symbol. The operator is the () operator. Notice that there's two sets of parentheses. The first set is part of the function name, operator (), and the second set is the arguments to that operator.

The operator () overload is called whenever you use an object for which it is defined as though it were a function. Consider:

struct greater_than_five
{
  bool operator()(int x) const { return x > 5; }
};

This class has operator () overloaded such that when you pass it an integer greater than 5, it returns true. You could use it like so:

greater_than_five f;
if (f(10)) {
  // 10 is greater than 5
}

Notice that although f is not a function and is an object of type greater_than_five, we can call it like a function, with f(10). This is a silly example, but demonstrates the use of such classes. These classes are commonly known as functors or function objects.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • the first idea that I had is that `()` is the operator that we overload but on the web page that I linked `()` is not in the list of the "Overloadable operators". – Roman Feb 26 '13 at 17:18
  • 1
    @Roman Yes it is - it's after `[]` and before `,`. Nonetheless, that site is not a reputable source of C++ information. – Joseph Mansfield Feb 26 '13 at 17:19
4

It means the instances of the class having that operator can be called with syntax like that of a function. This class can be referred to as a functor:

struct Foo
{
  bool operator () (int a) { std::cout << "Foo " << a << "!\n"; }
};

then

Foo f;
f(42); // prints "Foo 42!"

The operator() part means the operator can be invoked using (), and what follows is the parameter list, analogous to that of a plain function.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

The operator "()" is the parenthesis operator. You can overload it to take any type and any number of parameters. Here's an example:

class Vector3
{
public:
Vector3(float x, float y, float z)
{
    _data[0] = x;
    _data[1] = y;
    _data[2] = z;
}

float& operator() (const int component)
{
    assert(component >= 0 && component < 3);
    return _data[component];
}

private:
    float _data[3];
};

Usage:

Vector3 vec(3.0, 1.0, 2.0);
vec(2) = 0.5;
cout<<"Third component : "<<vec(2);

Prints 0.5

SNce
  • 2,453
  • 1
  • 16
  • 14