Question

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?

Était-ce utile?

La 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)); 

Autres conseils

This looks like OpenFOAM, which has its own hash table implementation.

If you look in src/OpenFoam/containers/HashTable/HashTable/HashTable.H, line 454 (at least in my copy), you'll find that iterator overloads operator() and operator* to return a reference to the iterator's currently referenced value.

To clarify a little, C++ allows you to overload many of the operators you use to provide domain-specific functionality. This allows for, say, vector.add(otherVector) to use the "obvious" syntactic sugar of vector + otherVector instead. The downside is that what is obvious is not always so obvious, as this question demonstrates.

This construction

cstrIter()(U, phi, transport, turbulenceModelName)

means that at first a temporary object of type cstrIter is created using the default constructor

cstrIter()

And after that the function call operator is used for this object

cstrIter()(U, phi, transport, turbulenceModelName)

That it would be more clear you could rewrite the expression the following way

cstrIter obj;
obj(U, phi, transport, turbulenceModelName);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top