I have this declaration
struct Z {
void operator ()( int a ) {
cout << "operator()() " << a << endl;
}
};
Z oz, *zp = &oz;
oz(1); //ok
(*zp)(2); //ok
zp(3); //"error: 'zp' cannot be used as a function"
Is there a way to modify struct declaration, so a call to No. 3 would succeed?
That’s expected behavior.
zpis a pointer (aZ *), andoperator()is overloaded forZ, notZ *. When you deference the pointer with*zp, you get aZ &, for whichoperator()is overloaded.Unfortunately, you can’t overload an operator for a pointer type (I think it has something to do with the fact that pointers are not user-defined types, but I don’t have the Standard in front of me). You could simplify the syntax with references: