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:

01
02
03
04
05
Class MyClass{
...
public:
  void operator()(int a, double b);
}
01
02
03
04
05
06
07
08
09
10
11
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