home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12503 < prev    next >
Encoding:
Text File  |  1992-08-18  |  1.2 KB  |  45 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!wupost!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc6.cs.uiuc.edu!pjl
  3. From: pjl@sparc6.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: Simple inheritance question
  5. Message-ID: <1992Aug18.160610.17129@sunb10.cs.uiuc.edu>
  6. Sender: news@sunb10.cs.uiuc.edu
  7. Organization: University of Illinois at Urbana-Champaign
  8. References: <schnitzi.714141577@eola.cs.ucf.edu>
  9. Distribution: usa
  10. Date: Tue, 18 Aug 1992 16:06:10 GMT
  11. Lines: 32
  12.  
  13. In <schnitzi.714141577@eola.cs.ucf.edu> schnitzi@cs.ucf.edu (Mark Schnitzius) writes:
  14.  
  15. >Is there any way to access the variable 'hold' in
  16. >this example with only a pointer to the base class?
  17.  
  18. >class base {};
  19.  
  20. >class derived: public base {
  21. >    int hold;
  22. >};
  23.  
  24. >main ()
  25. >{
  26. >    derived d;
  27. >    base *p;
  28.  
  29. >    p = &d;
  30.  
  31. >    cout << p->hold;  //DOESN'T WORK!
  32. >}
  33.  
  34. *****>    cout << ((derived*)p)->hold;
  35.  
  36.     However, you had better be *sure* that 'p' really, really does
  37.     point to a 'derived'.
  38.  
  39.     The reason it doesn't work is because 'base' objects don't have
  40.     'hold' variables; hence, the required cast.
  41. -- 
  42.     - Paul J. Lucas                University of Illinois    
  43.       AT&T Bell Laboratories        at Urbana-Champaign
  44.       Naperville, IL            pjl@cs.uiuc.edu
  45.