Using the () operator in member functions

I overloaded the operator () in one of my classes, and I would like to use it in another member function.

class A { public: void operator()(); void operator()(double x); }; void A::operator()() { // stuff }; void A::operator()(double x) { // stuff with other members and x this->operator(); }; 

The string this->operator() does not work. I just want to use the operator that I defined as a member function of my class A The error I get is: Error 1 error C3867: 'A::operator ()': function call missing argument list; use '&A::operator ()' to create a pointer to member Error 1 error C3867: 'A::operator ()': function call missing argument list; use '&A::operator ()' to create a pointer to member

+4
source share
3 answers

You must write:

 void A::operator()(double x) { // stuff with other members and x this->operator()(); }; 

The first () is the name of the statement, and the second is for the call itself: a list of missing (empty) arguments from the error message.

+9
source

The syntax for calling a member function in an object pointer is:

 ptr->memberName(); 

In your case, the operator() name is operator() , so you want to write

 ptr->operator()(); 

or for another option

 ptr->operator()(1.0); // operator() accepting single argument of type double 

The same applies to the definition of operator() - you are missing one pair of parent points.

0
source

General rule:

The operator overload function name is always operator@ , while @ is an overloaded operator.

To call the operator overload directly by its name, enter operator@ (params) in your case operator() (...) .

0
source

All Articles