473,320 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
Apr 2 '07 #1
5 1118
The Cool Giraffe wrote:
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.
Apr 2 '07 #2
On 2 Apr, 21:52, "The Cool Giraffe" <giraf...@viltersten.comwrote:
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};

Apr 2 '07 #3
The Cool Giraffe <gi******@viltersten.comwrote:
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
--
Keith Davies "Sometimes my brain is a very strange
ke**********@kjdavies.org to live in."
ke**********@gmail.com -- Dana Smith
http://www.kjdavies.org/
Apr 2 '07 #4
The Cool Giraffe wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #5
"The Cool Giraffe" <gi******@viltersten.comwrote in message
news:57*************@mid.individual.net...
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;

}
Apr 2 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Larry Woods | last post by:
I have a site that works fine for days, then suddenly, I start getting ASP 0115 errors with an indication that session variables IN SEPARATE SESSIONS have disappeared! First, for background...
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
1
by: Steve Remer | last post by:
My application (relevant code snippets below) originally used Session variables in order to maintain state from page to page. After being unable to solve the mystery of why those variables were...
18
by: BillE | last post by:
When a user opens a new IE browser window using File-New-Window the integrity of an application which relies on session state is COMPLETELY undermined. Anyone who overlooks the fact that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.