N
Noah Roberts
The said:Suppose there's an abstract base class and two inheriting
classes declared as follows.
class Shape {double sizeA;};
class Ellipse : Shape {double sizeB};
class Rect : Shape {double sizeC};
In our program we will have a pointer to a shape but we
don't know which one yet. So, we declare it as follows.
Shape *shapy;
Then, i'd like to do this:
shapy = new Ellipse ();
shapy->sizeA = 4;
shapy->sizeB = 5;
or this:
shapy = new Rect ();
shapy->sizeA = 4;
shapy->sizeC = 5;
but, while the first two lines work fine (i.e. the computer
finds the sizeA and can handle the pointers to Ellipse
and Rect),
Then your compiler is broken. with the second line you do not have access.
the implementation specific variables are not
reachable. How can i solve this without binding shapy to
Ellipse or Rect explicitly?
You can't...well, you could do something stupid like trying to guess the
location of that variable but it would pretty much be the same thing.