Andry
Andry

Reputation: 16845

Calling overloaded operator () from object pointer

Consider the following:

class MyClass {
public:
   int operator ()(int a, int b);
};

When having:

MyClass* m = new MyClass();

I want to access the operator() method, so I could:

(*m)(1,2);

But can I do this?

m->(1,2);

Upvotes: 47

Views: 24713

Answers (2)

KinGamer
KinGamer

Reputation: 509

If you won't change m (what it points to), you can substitute (*m) by a reference:

MyClass *m = new MyClass();
MyClass &r = *m;
r(1, 2);

See this answer for more details.

Upvotes: 8

Luchian Grigore
Luchian Grigore

Reputation: 258618

Not with that syntax, but you can do

 m->operator()(1,2);

Upvotes: 80

Related Questions