Code, Notes

C++ – Calling Overloaded Operator () from Pointer

Here are three ways to call a C++ classes’ operator () using a pointer:
Say you have a C++ class name MyClass that overloads the operator “()”, something along the lines:

Class MyClass{
...
public:
  void operator()(int a, double b);
}
MyClass* theClass = new MyClass;

// method 1: use a reference
MyClass& classRef = *theClass;
classRef(a, b);

// method 2: dereference in place
(*theClass)(a,b);

// method 3: dereference the ugly way:
theClass->operator()(a,b);

I use method 2.

Leave a Reply