下面的代码在c++中是什么意思?
struct Dog{
int a;
int b;
};
int Dog::*location = &Dog::a
Dog* obj1 = new Dog;
obj1->*location = 3;
&Dog::a
指的是什么?
struct Dog{
int a;
int b;
};
int Dog::*location = &Dog::a
Dog* obj1 = new Dog;
obj1->*location = 3;
what does &Dog::a
refer to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它创建一个指向成员的指针,这就像一个指向类的数据成员的指针,但类实例尚未确定,它只是偏移量。 (请注意,当与多重继承或虚拟继承结合使用时,它比简单的偏移量要复杂得多。但是编译器会计算出详细信息。)
请注意指向成员的指针取消引用运算符
->*< /code> 在最后一行中使用,其中类实例与指向成员的指针组合以生成特定实例的特定数据成员。
It creates a pointer-to-member, which is like a pointer to a data member of a class, but the class instance isn't determined yet, it's just the offset. (Note, when combined with multiple inheritance or virtual inheritance, it gets quite a bit more complicated than a simple offset. But the compiler works out the details.)
Notice the pointer-to-member dereference operator
->*
used in the last line, where the class instance is combined with the pointer-to-member to yield a particular data member of a particular instance.变量
location
被称为“成员数据指针”。它是指向结构内部某些内容的指针,但除非与实际对象指针一起使用,否则没有意义。单独使用*location
不足以解析实际地址,但obj1->*location
指的是实际位置。The variable
location
is known as "member data pointer". It's a pointer to something inside a structure, but doesn't make sense unless it's used with an actual object pointer. The use of*location
by itself would not be enough information to resolve to an actual address, butobj1->*location
refers to an actual location.&意思是获取某物的地址。
& means take the adress of something.