简体   繁体   English

指向具有继承类型的类数据成员的指针

[英]Pointer to class data member having inherited type

I'd like to use a pointer to a class data member that has an inherited type. 我想使用一个指向具有继承类型的类数据成员的指针。 The code is pretty straightforward, I have an object (A) with a data member which class (Integer) inherits another class (Type), and I'd like to create a pointer to this data member using the parent class (Type*) instead of the base class (Integer) : 该代码非常简单,我有一个带有数据成员的对象(A),该类的成员(Integer)继承了另一个类(Type),并且我想使用父类(Type *)创建一个指向该数据成员的指针而不是基类(整数):

class Type
{
public:
    Type() {}
};

class Integer : public Type
{
public:
    Integer() : Type() {}
    int value;
};

class A
{
public:
    A() { p_value.value = 0; };
    Integer p_value;
};

int main()
{
    Type* aType = &A::p_value;
    return 0;
}

the code on ideone ideone上的代码

I have a compilation error: 我有一个编译错误:

error line: Type* aType = &A::p_value;
error: cannot convert 'Integer A::*' to 'Type*' in initialization

After a lot of fiddling with the code I couldn't make it work, what am I doing wrong ? 经过对代码的大量摆弄后,我无法使其正常工作,我在做什么错呢?

Basically types of pointers aren't matching. 基本上,指针的类型不匹配。 &A::p_value is of type A::*Integer (or Integer A::* as compiler nicely states), while Type* is... just Type* . &A::p_value的类型为A::*Integer (编译器很好地指出是Integer A::* ),而Type*是...只是Type*

Former ( A::*Integer ) is a pointer to member and can be used like this: 前一个( A::*Integer )是指向成员的指针,可以这样使用:

A a1;
A* a2 = new A; 
A::*Integer a_member = &A::p_value;
a1.*a_member = 1;
a1->*a_member = 2;
// now a.p_value == 1 a->p_value == 2

Meanwhile Type* is just pointer to Type object - since Type class hierarchy is separate from A class hierarchy no casting between classes make sense. 同时, Type*只是指向Type对象的指针-因为Type类层次结构与A类层次结构是分开的,所以在类之间进行强制转换是没有意义的。 And here you want to cast from pointer to member to pointer to class. 在这里,您要从成员的指针转换为类的指针。 You most likely wanted to to something like this: 您最有可能想要这样的事情:

int main()
{
    A a;
    Type* aType = &a.p_value;
    return 0;
}

Difference lies in what they do. 差异在于他们的工作。 Pointer to class will look up in memory beginning of your object, then use information about where in this object particular member/virtual function pointer lies, go there and return value. 指向类的指针将在对象的内存中查找,然后使用有关特定成员/虚拟函数指针在该对象中位于何处的信息,然后到达那里并返回值。 We use them to obtain data or call virtual method basing on object's address. 我们使用它们来获取数据或基于对象的地址调用虚拟方法。

In the other hand pointer to member only knows the difference between beginning of object's memory and sought data position. 另一方面,指向成员的指针仅知道对象的存储开始与所查找的数据位置之间的差异。 So you need to have both object's address and the delta to figure out location of the data: 因此,您需要同时拥有对象的地址和增量,才能确定数据的位置:

(object_by_value).*(pointer_to_member);
(object_by_pointer)->*(pointer_to_member);

By the way they work you can understand that casting one into another will only produce garbage and compiler saves you from that mistake with a warning: 通过它们的工作方式,您可以理解将其强制转换为另一种垃圾只会产生垃圾,并且编译器会警告您避免该错误:

cannot convert 'Integer A::*' to 'Type*' in initialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM