why we don't make pointer of child/derived class which points to base class ?

Sep 21, 2013 at 4:47am
why we don't make pointer of child/derived class which points/assigns to base class object?
for example:

1
2
3
4
parent a; // a is a parent class object!
child * p = & a; // p is child class pointer !



why we do not do this in c++ ?
Last edited on Sep 21, 2013 at 6:36pm
Sep 21, 2013 at 11:28pm
Parent is not a child. It does not have the interface of a child.

A question for you. What will "s" contain:
1
2
3
T a;
T * p = &a;
T * s = p+1;
Sep 22, 2013 at 9:55am
if we print value of p it will print 00cofefc on console
if we print value of s it will print 00coff00 on console !
Sep 22, 2013 at 11:11am
Yes, but what does that mean? Btw, what was the T in your test?
Last edited on Sep 22, 2013 at 11:12am
Sep 22, 2013 at 11:35am
I am not very much familiar with hex numbers so i am not sure what that does mean !
you may explain this to me !

T is a class name/user defined data type (parent class) !
Sep 22, 2013 at 12:09pm
Repeat with these then:
1
2
3
4
5
6
7
class A {
  int a;
};

class B : public A {
  int b;
}

Topic archived. No new replies allowed.