home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!wupost!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc6.cs.uiuc.edu!pjl
- From: pjl@sparc6.cs.uiuc.edu (Paul Lucas)
- Subject: Re: Simple inheritance question
- Message-ID: <1992Aug18.160610.17129@sunb10.cs.uiuc.edu>
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <schnitzi.714141577@eola.cs.ucf.edu>
- Distribution: usa
- Date: Tue, 18 Aug 1992 16:06:10 GMT
- Lines: 32
-
- In <schnitzi.714141577@eola.cs.ucf.edu> schnitzi@cs.ucf.edu (Mark Schnitzius) writes:
-
- >Is there any way to access the variable 'hold' in
- >this example with only a pointer to the base class?
-
- >class base {};
-
- >class derived: public base {
- > int hold;
- >};
-
- >main ()
- >{
- > derived d;
- > base *p;
-
- > p = &d;
-
- > cout << p->hold; //DOESN'T WORK!
- >}
-
- *****> cout << ((derived*)p)->hold;
-
- However, you had better be *sure* that 'p' really, really does
- point to a 'derived'.
-
- The reason it doesn't work is because 'base' objects don't have
- 'hold' variables; hence, the required cast.
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-