Calling a method with no name

Viewed 94

I have this line that I need to be able to run with the code I am writing:

 euro(2, Regular)

euro is an object from a class named MainControl, and I have an enum defined:

enum VoterType { All, Regular, Judge };

Basically I understand that we want to use some method from the class named MainControl, but I don't see the name of the method that should be used?

Given the fact that this function returns void, how exactly is the declaration supposed to look like in the class MainControl?

1 Answers

MainControl class is implementing a custom overloaded operator, i.e., "function call operator".

How exactly is the declaration supposed to look like in the class MainControl?

The method signature should be something similar to:

class MainControl {
  // ...
 public:
  void operator()(int, VoterType);
};

When a user-defined class overloads the function call operator, operator(), it becomes a FunctionObject type.

In short, your class instance (euro) is a FunctionObject and that particular method can be simply invoked as (for example):

euro(2, VoterType::Regular)

Additional Notes

Just for sake of completeness, the "operator-method" is just like another method (no black magic here).

Indeed, you can even invoke it with the complete name:

euro.operator()(2, VoterType::Regular);

But the syntax becomes pretty ugly.

Related