[英]What does the following code mean in c++?
struct Dog{
int a;
int b;
};
int Dog::*location = &Dog::a
Dog* obj1 = new Dog;
obj1->*location = 3;
what does &Dog::a
refer to? &Dog::a
指的是什么?
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. 注意在最后一行中使用的指向成员的解引用操作符
->*
,其中类实例与指向成员的指针组合以产生特定实例的特定数据成员。
The variable location
is known as "member data pointer". 变量
location
称为“成员数据指针”。 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, but obj1->*location
refers to an actual location. *location
本身的使用不足以解析为实际地址,但是obj1->*location
指的是实际位置。
&意味着接受某事的地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.