What does the "&SomeStruct::member" mean in C++ where "SomeStruct" is a struct and "member" is its member?
Say SomeStruct
is defined as:
struct SomeStruct {
int member;
};
What does these means?
&SomeStruct::member
int SomeStruct::*
I encounter this, tried to output its type info but still cannot figure out the meaning. Belo开发者_JAVA百科w is a working example:
#include <iostream>
#include <typeinfo>
using namespace std;
struct SomeStruct {
int member;
};
int main(int argc, const char *argv[])
{
cout << typeid(&SomeStruct::member).name() << endl;
cout << typeid(int SomeStruct::*).name() << endl;
return 0;
}
Compiled by i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
on my MBP, the output is:
M10SomeStructi
M10SomeStructi
int SomeStruct::*
is called a "pointer to member", in this case, a pointer to a member of SomeStruct
. This is NOT strictly a pointer to a member function (although this is the most common use of this syntax).
&SomeStruct::member
is a reference to the member
member of SomeStruct
.
See this related question
If you'd like some more complete information on the topic, here's a decent article on the topic.
And, the obligatory section on the topic in the C++ FAQ lite.
This is the syntax for a pointer to a member function/data member.
int SomeStruct::*
is the type of the pointer (pointer to an int
data member of SomeStruct
.
&SomeStruct::member
yields a pointer of the above type.
精彩评论