Hi all,
I'm writing a Visual C++ 6.0 program, and I have a problem with polymorphism.
Suppose that you have defined the classes:
class BasicClass{
public:
int num_Basic;
};
class Deriv1Class : public BasicClass{
public:
int num_Deriv1;
};
class Deriv2Class : public BasicClass{
public:
int num_Deriv2;
};
and suppose you have defined a pointer
BasicClass *p;
For the polymorphic rules this pointer can point to BasiClass, Deriv1Class or Deriv2Class objects.
Now, suppose that at a certain point of the code you know that p is pointing to a Deriv1Class object,and you want to read its (public) num_Deriv1 member.
If you try to access directly to the member (with a statement as p->num_Deriv1), an error occurs, because num_Deriv1 is not a member of BasicClass (the class of p definition).
Indeed, if you define a temporary object of Deriv1Class and you write:
Deriv1Class tempDeriv1Obj;
tempDeriv1Obj = *p;
int n = tempDeriv1Obj.num_Deriv1;
another error occurs, because it isn't possible to convert a type from BasicClass to Deriv1Class.
At this point my question is:
if you have a pointer like that, is there a way to read this blessed num_Deriv1 member of the Deriv1Class object pointed?
Thank you very much
I'm writing a Visual C++ 6.0 program, and I have a problem with polymorphism.
Suppose that you have defined the classes:
class BasicClass{
public:
int num_Basic;
};
class Deriv1Class : public BasicClass{
public:
int num_Deriv1;
};
class Deriv2Class : public BasicClass{
public:
int num_Deriv2;
};
and suppose you have defined a pointer
BasicClass *p;
For the polymorphic rules this pointer can point to BasiClass, Deriv1Class or Deriv2Class objects.
Now, suppose that at a certain point of the code you know that p is pointing to a Deriv1Class object,and you want to read its (public) num_Deriv1 member.
If you try to access directly to the member (with a statement as p->num_Deriv1), an error occurs, because num_Deriv1 is not a member of BasicClass (the class of p definition).
Indeed, if you define a temporary object of Deriv1Class and you write:
Deriv1Class tempDeriv1Obj;
tempDeriv1Obj = *p;
int n = tempDeriv1Obj.num_Deriv1;
another error occurs, because it isn't possible to convert a type from BasicClass to Deriv1Class.
At this point my question is:
if you have a pointer like that, is there a way to read this blessed num_Deriv1 member of the Deriv1Class object pointed?
Thank you very much