Variables disappearing from scope when i don't want them to

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.
 
T

The Cool Giraffe

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), the implementation specific variables are not
reachable. How can i solve this without binding shapy to
Ellipse or Rect explicitly?
 
G

gw7rib

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), the implementation specific variables are not
reachable. How can i solve this without binding shapy to
Ellipse or Rect explicitly?

Have you tried:

class Shape {public: double sizeA;};
class Ellipse : public Shape {public: double sizeB};
class Rect : public Shape {public: double sizeC};
 
K

Keith Davies

The Cool Giraffe 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), the implementation specific variables are not
reachable. How can i solve this without binding shapy to
Ellipse or Rect explicitly?

This isn't the code you tried to compile. Your real code almost
certainly makes sizeA, sizeB and sizeC public members, or you wouldn't
have gotten as far as you had.

Anyway, back to your question:

Without casting, you mean? You can't.

As far as the compiler knows, 'shapy' is a Shape*. It has no knowledge
whatsoever that it may in fact be an Ellipse or Rect. It knows only the
interface to Shape, which has sizeA. To get at sizeB you must have an
Ellipse, to get at sizeC you must have a Rect.

You can get this by doing:

Ellipse *e = new Ellipse();
e->sizeA = 4;
e->sizeB = 5;
shapy = e;


Keith
 
V

Victor Bazarov

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};

'Ellipse' inherits from 'Shape' privately. That means nobody
can use this inheritance except 'Ellipse'.
class Rect : Shape {double sizeC};

Same here. Nobody can use the derivation properties except
'Rect' itself.
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 ();

Conversion from 'Ellipse*' to 'Shape' is not allowed, except
in a member of 'Ellipse'.
shapy->sizeA = 4;
shapy->sizeB = 5;

or this:

shapy = new Rect ();

Same here. This conversion requires an _accessible_ base
class. Your 'Shape' is _inaccessible_ in '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),

Huh? I can understand 'shapy->sizeA' would be OK, but conversion
from 'Ellipse*' to 'Shape*' is NOT allowed.
the implementation specific variables are not
reachable. How can i solve this without binding shapy to
Ellipse or Rect explicitly?

There is no way.

V
 
J

Jim Langston

The Cool Giraffe 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), the implementation specific variables are not
reachable. How can i solve this without binding shapy to
Ellipse or Rect explicitly?

You have to dyanic_cast it. Fixed the many errors in your code and am
showing how it could be done. It could probably be done with dynamic
casting to a reference or something too.

#include <iostream>
#include <string>

class Shape
{
public:
double sizeA;
virtual ~Shape() {}
};

class Ellipse: public Shape
{
public:
double sizeB;
};

class Rect: public Shape
{
public:
double sizeC;
};

int main()
{
Shape *shapy;

shapy = new Ellipse ();
shapy->sizeA = 4;
if ( dynamic_cast<Ellipse*>( shapy ) != NULL )
dynamic_cast<Ellipse*>( shapy )->sizeB = 5;

delete shapy;

shapy = new Rect ();
shapy->sizeA = 4;

Rect* ThisRect = dynamic_cast<Rect*>( shapy );
if ( ThisRect != NULL )
ThisRect->sizeC = 5;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top