mardi 3 mars 2015

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) :



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


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 ?


Aucun commentaire:

Enregistrer un commentaire