2
class functor;

functor();

What is () operator called? Does it create objects using new operator?

I tried searching on google, but "()" was absent from the result, even with quotes.

Edit : () is used with functors generally, but it can be used with classes who have not overloaded the operator (). It creates object of the class.

q126y
  • 1,589
  • 4
  • 18
  • 50

4 Answers4

4

It is known as the CALL operator it is written after a function's name during a function call. Arguments to the function are passed in this operator (if there are any, that is).

incomplet_
  • 318
  • 2
  • 10
  • what it is known as, when used after class, and the class has not overloaded `()` operator. – q126y Dec 08 '15 at 04:04
  • In that case the line you wrote after the class name is just the function declaration. The () operator still remains the same, however it will come in handy when you wish to overload this function, in that case if you pass some arguments in the () operator it will overload the given function. Therefore the () operator remains the same (however one of its functionalities is helping in overloading a function, but it still remains the CALL operator, which contains the arguments to a function) – incomplet_ Dec 08 '15 at 04:10
  • No, it is not. It creates object of the class when used, with a class – q126y Dec 08 '15 at 04:14
  • Note that the code you have used uses a constructor i.e `functor();` Now it is important to remember that constructors are member functions too, the only way they differ from other member functions is that they have the same name as the class and no return type and perform the specific task of initializing the data members. But the fact remains that they too are functions and hence () remains a **CALL operator** and its meaning does not change because it is used with a constructor which is just another type of member function since it is still a function – incomplet_ Dec 08 '15 at 15:06
  • Sorry for the late reply had to do some research to answer that one, good question though, hope you are satisfied with the answer – incomplet_ Dec 08 '15 at 15:07
  • Like you said, constructors don't return anything, but in this case, it returns object of the given class. – q126y Dec 10 '15 at 06:07
3

When used with objects, it's generally referred to as the "function call operator", which invokes the "operator ()" of that object. Or, if the object is a plain function, reference to a function or pointer to a function, it simply calls that function.

If used with type names, the syntax T() invokes the default constructor of that type, creating an unnamed object of that type. The () in that construct is simply referred to as "the initializer".

See section 8.5 "initializers" of the C++ 11 standard.

Paul Groke
  • 6,259
  • 2
  • 31
  • 32
  • even when used after class, and the class has not overloaded `()` operator? It creates object of the class when used, with a class. – q126y Dec 08 '15 at 04:05
  • 1
    Oh, I'm sorry, seems I didn't understand your question correctly. If you had chosen another name (not "functor") I might have understood. Anyway, when invoking a constructor I'm not sure that the "()" is called an operator at all. The parenthesis following the class name are simply called the "initializer" (in your example an empty initializer). – Paul Groke Dec 08 '15 at 04:29
  • okay, but the initializers(as used in base class initialization) don't return anything. But, in this case, it returns an object of the same class. – q126y Dec 10 '15 at 06:06
1

In your example, where functor is a type, opposed to an object, the expression functor() is called an explicit type convertion in functional notation. A temporary object of type functor is created. The new operator is not involved in any way. The exact grammar of the explicit type convertion expression is simple-type-specifier ( expression-list-opt ), typename-specifier ( expression-list-opt ). Note that I used the suffix -opt opposed to the subscribt opt as the standard does it. See 5.2.3 Explicit type conversion (functional notation) [expr.type.conv] in the C++14 standard, or here on cppreference.

If the left hand side of ( is an expression, as in functor f; f(), the expression f() is a function call. The exact grammar is postfix-expression ( expression-list-opt ). See 5.2.2 Function call [expr.call] in the C++14 standard, or here on cppereference. Here you would say that the function call operator is used. The type of f, here functor, must overload the function call operator, see e.g. chapter 'Function call operator' here on cpprefrence. You might also want to read the page Function objects on cppreference.

Florian Kaufmann
  • 803
  • 6
  • 13
0

Its function call operator overloading.

http://www.tutorialspoint.com/cplusplus/function_call_operator_overloading.htm

Related article.

Why override operator()?

Community
  • 1
  • 1
Nit
  • 22
  • 3