Reputation: 179
This problem is caused when I want to get the address of Functions in Macros after the sign(::
).It always has a trailing whitespace which reports an error.
I'm using G++ in Mac OS Mojave i386.
For example, I have the class:
struct A{
void b();
};
and there's a macro:
#define ACC(name) &A::name
I use this to get the pointer of A::b
:
void(*accab)() = ACC(b);
And I will get the error like this:
error: cannot initialize a variable of type 'void (*)()' with an rvalue of type 'void (A::*)()'
After that I tried putting a bracket but I got this:
error: call to non-static member function without an object argument
Is there any way to solve the problem so that the pointer can be gotten by macro?
Upvotes: 1
Views: 165
Reputation: 26800
The type of a pointer to function is different from the type of pointer to member function.
In general, the type of pointer to any member is distinct from the type of a normal pointer.
To make this work you will have declare accab
as pointer to member:
void(A::* accab)() = ACC(b);
The C++ standard has a note concerning this:
The type “pointer to member” is distinct from the type “pointer”, that is, a pointer to member is declared only by the pointer to member declarator syntax, and never by the pointer declarator syntax.
Upvotes: 3