Reputation: 103
This is the sample code:
typedef int (T::*t1)(long);
typedef int (*T::*t2)(long);
t1 p1 = NULL;
t2 p2 = NULL;
The type t2 get's extra * (by mistake), but this code compile and run OK. But when t2 is not NULL, it gets error:
struct T
{
int func(long) {return 0;}
};
t2 p2 = &T::func;
error: cannot convert ‘int (T::*)(long int)’ to ‘t2’ {aka ‘int (* T::*)(long int)’} in initialization
I'm curious, what's the real type of t2? How can I use t2?
Upvotes: 4
Views: 66
Reputation: 170202
Eliminate the pointer-to-member bit of syntax (by applying the spiral rule heuristic), and a specific type stands out:
int (* /* T::*t2 */)(long)
-> int (*)(long)
.
So t2
is a pointer to a member of T
, which is itself a pointer to a free function.
int bar(long) { return 0; }
struct T
{
int (*func) (long) = &bar;
};
t2 p2 = &T::func;
Upvotes: 5