I have the following classes:

class A { public: B* b; } class B { public: double operator()(double val); } 

Then at some point I try to call class B as a function through a pointer to class A :

 a->b(val); //a - указатель на экземпляр класса A 

When I compile, I get the error:

 error: 'A::b' cannot be used as a function 

Question: how to fix it? Those. How to use pointer functors?

  • (a->b)(val) - the same thing - Andrei Kurulev
  • Oh wait a minute, you have a pointer to B , not just B Arrow dereference only a . No compiler at hand, no way to check. - D-side
  • For sure. If you explicitly dereference everything, then it is compiled: (*(a->b))(val) . Thank. - Andrey Kurulev
  • Oh, you did it yourself. Issue and answer then =) - D-side

2 answers 2

Could be so:

 a->b->operator()(val); 

if a is a pointer to A

If, as you wrote in the question, // a is an instance of class A , then

 (*ab)(val); ab->operator()(val); 

Judging by your answer to your question, you still made a mistake in the question itself, and a - a pointer to the instance, and not the instance itself ...

  • Yes, indeed, I was mistaken in the question a little. Corrected. - Andrey Kurulev

In general, in order to compile, you must explicitly dereference the pointer a->b :

 (*(a->b))(val); //Вместо a->b(val)