Reputation: 75
Here is what I have:
A class PostfixCalculator, with public member methods:
class PostfixCalculator
{
public:
PostfixCalculator();
int top();
int popTop();
void pushNum(int);
void add();
void minus();
void multiply();
void divide();
void negate();
bool empty();
void pushSymbol(string);
and when I try to call a member function by pointer to member function, I tried something like the following (I know the method does not make much sense, it is just a test):
void PostfixCalculator::pushSymbol(string str)
{
func f = &PostfixCalculator::add;
this.*(f)();
}
However, I get the following compiler error:
> postfixCalculator.cpp:84:12: error: called object type 'func' (aka
> 'void (PostfixCalculator::*)()') is not a function or function pointer
> this.*(f)();
> ~~~^ 1 error generated.
I am using clang++ to compile my program, under fedora linux.
Upvotes: 0
Views: 929
Reputation: 320481
Firstly, this
is a pointer, meaning you have to apply ->*
to it, but not .*
. If you want to use .*
, you have to dereference this
with *
first.
Secondly, function call operator ()
has higher priority than .*
or ->*
operators, meaning that you need extra parentheses to make sure that the pointer f
is dereferenced first, and the function call ()
is applied to the result of that dereference.
Should be
(this->*f)();
or alternatively
(*this.*f)();
Upvotes: 2
Reputation: 852
this
is a pointer so you should use ->
and deref * have lower priority than function call ()
, so you should use (this->*f)()
Upvotes: 0