weird form of function parameter

What does this snippet below mean ?

1
2
3
4
5
6
7
8
9
10
11
12
struct C {
    C( int i ) : data(i) { }
    int data;
};

template <class T> void f( T C::* ) // <== What does this parameter mean ?
{ }

int main()
{
    f( &C::data ); // how come you got the address of a non-static member
}


I was thinking of it as an unnamed pointer, am i right ?
The snippet makes no sense. The syntax is for pointer to member.
in main(), how come you can get the address of a non-static member ?
&C::data is not the address of a non-static member, it's a pointer to member. int C::* is its type

If you have an object of type C or derived from C, you can use that pointer to access the member:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

struct C {
    C( int i ) : data(i) { }
    int data;
};

int main()
{
    int C::* p = &C::data; // pointer to member

    C obj1(7), obj2(-1);
    std::cout << obj1.*p << ' ' << obj2.*p << '\n';
}


a bit more detail: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_data_members
Last edited on
ohh, i see, big thanks to both of you for a new knowledge :DD
Topic archived. No new replies allowed.