home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13123 < prev    next >
Encoding:
Text File  |  1992-08-31  |  1.5 KB  |  40 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Multiple Inheritance Problem
  5. Message-ID: <9224511.24381@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1992Aug31.230311.4668@speedy.aero.org>
  9. Date: Tue, 1 Sep 1992 01:37:06 GMT
  10. Lines: 28
  11.  
  12. don@surtsey.aero.org (Don C. Hancock) writes:
  13.  
  14. >I'm confused as to why the following results in what to me is the wrong
  15. >virtual function being called.  I'm sure the problem is somehow caused
  16. >by my casting the result of the function call from one base to the
  17. >other (and somehow lying to the compiler), but I'm not sure how else to
  18. >accomplish my task.
  19.  
  20. Yes, that is exactly what is causing the problem.
  21. You can fix it by inserting an extra cast to (derived *) before
  22. casting to (base2 *), ie.
  23.  
  24.     main() {
  25.         base2 *bb2 = (base2 *)(derived *)fun();
  26.         bb2->v2();    // prints der::v2 as desired
  27.     }
  28.  
  29. BUT, I would strongly recommend *against* using these sort of casts.
  30.  
  31. Maybe you could explain in a bit more detail why you think that you
  32. need to downcast from a (base *) to a (base2 *), and then we may
  33. be able to give you an alternative solution :-)
  34.  
  35. -- 
  36. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  37. This .signature virus is a self-referential statement that is true - but 
  38. you will only be able to consistently believe it if you copy it to your own
  39. .signature file!
  40.