c++constructorparentheses

What do two parentheses after a variable signify?


I have something like this in one method

    autoPtr<LESModel> LESModel::New   
 95 (
 96  const volVectorField& U,
 97  const surfaceScalarField& phi,
 98  transportModel& transport,
 99  const word& turbulenceModelName
100 )
101 {
     ...
122  dictionaryConstructorTable::iterator cstrIter =                       
123  dictionaryConstructorTablePtr_->find(modelType);
     ...
143  return autoPtr<LESModel>      
144  (
145  cstrIter()(U, phi, transport, turbulenceModelName)
146  ); 
147  }

If I am right cstrIter is a variable of class dictionaryConstructorTable::iterator (could not find this class though) and beginning with line 143 the constructor autoPtr<LesModel> is called and the result returned. The parentheses after the constructor autoPtr<LESModel> therefore should be parameters and since cstrIter is a variable I am wondering what the two parentheses after the variable mean. Perhaps I am misreading something?


Solution

  • C++ supports 'operator overloading', meaning you can define types that support syntax like a + b. This works by defining functions with names such as operator+. When an overloadable operator is used with a user defined type C++ looks for functions with these special names and, if a suitable function is found, treats the operator as a function call to the function.

    One of the operators that one can overload is the function call operator. A member function named operator() will be called when you use an object name as though it's a function:

    struct S {
      void operator() (void) {
        std::cout << "Hello, World!\n";
      }
    };
    
    int main() {
      S s;
      s(); // prints "Hello, World!\n"
    }
    

    It looks like dictionaryConstructorTable::iterator overloads the function call operator and returns some type that also overloads the function call operator (or just uses the built-in operator).

    Replacing the use of the function call operator with normal member functions may make it clearer what's happening:

    return autoPtr<LESModel>( cstrIter.foo().bar(U, phi, transport, turbulenceModelName));