In the C++17 standard draft document, in subsection (6.7.3) "CV-qualifiers" [basic.type.qualifier]: paragraph 6, there is this sentence:
For a class type C, the type corresponding to the type-id
void(C::* volatile)(int) const
has the top-level cv-qualifier volatile.
I have C language background and was randomly reading C++ standard and can't make sens of this, I tried to interpret it as
a volatile function pointer to a function that can be called on const objects and that takes a single int argument and return a ??? ( C type / void I am lost here)
does someone have an explanation ?
I have tried this code
int main()
{
class C
{
public:
typedef void(C:: * volatile X)(int)const ;
void f(int z) const
{cout << z << endl;}
X a = (X)&f; // had to add -fpermissive flag
};
C t;
t.f(5); // works obviously
(t.a)(5); // gives the following compilation message
main.cpp:22:18: warning: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say ‘&main()::C::f’ [-fpermissive]
X a = (X)&f;
^
main.cpp:27:12: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘t.main()::C::a (...)’, e.g. ‘(... ->* t.main()::C::a) (...)’
(t.a)(5);
^
changing to what is adviced doesn't work !
thank you for your help
The C::*
syntax denotes a member-function pointer which means a pointer to a member-function (sometimes called a method) that belongs to a class C
.
In order to obtain an address of a member-function you need to use special syntax with qualified member-function name:
X a = &C::f;
In order to call a member-function via pointer you need to use a special operator .*
(as your compiler advises):
(t.*(t.a))(5);
Note that we need to use t
twice: for accessing a
member and for calling a
with t
as this
.