C++中,"SomeStruct"是结构,"member"是其成员,"&SomeStruct::member"是什么意思?
What does the "&SomeStruct::member" mean in C++ where "SomeStruct" is a struct and "member" is its member?
假设SomeStruct
定义为:
struct SomeStruct {
int member;
};
这些是什么意思?
-
&SomeStruct::member
-
int SomeStruct::*
我遇到这个,试图输出它的类型信息,但仍然不能弄清楚的含义。下面是一个工作示例:
#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;
}
由i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
在我的MBP上编译,输出为:
M10SomeStructi
M10SomeStructi
int SomeStruct::*
被称为"指向成员的指针",在本例中是指向SomeStruct
成员的指针。严格来说,不是是指向成员函数的指针(尽管这是该语法最常用的用法)。
&SomeStruct::member
是对SomeStruct
成员member
的引用。
参见相关问题
如果你想了解更多关于这个主题的完整信息,这里有一篇关于这个主题的不错的文章。
并且,c++ FAQ生活中关于该主题的强制性部分
这是指向成员函数/数据成员的指针语法。
int SomeStruct::*
是指针的类型(指向SomeStruct
的int
数据成员的指针)。
&SomeStruct::member
返回上述类型的指针
相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- Centos7 g++ "to_string is not in a member of std"
- C++复制函数重载导致"must be a nonstatic member function"错误
- C++ "Invalid use of 'this' in non-member function" ,
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 使用 GraphicsPath::AddString() 抱怨"class 'GraphicsPath' has no member 'AddString'"
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 为什么我必须在初始化 std::array<SomeStruct, size> 时指定每个项目的类型C++
- 当我编译XG-Bosst时,出现错误"error: 'Split' is not a member of 'xgboost::common' ……"
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- 使用线程从另一个成员函数调用一个Member函数
- 为什么"non-standard syntax; use '&' to create a pointer to member"在 CTOR 中使用线程?
- 尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."
- 为什么我会收到此警告? "Member 'x' was not initialized in this constructor"
- std::p riority_queue<struct with const member&> - 'operator =' 函数不可用
- Protobuf 生成的C++类无法针对 iOS 进行编译,并显示错误"Only virtual member functions can be marked 'final'"
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- C++中,"SomeStruct"是结构,"member"是其成员,"&SomeStruct::member"是什么意思?