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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!news.aero.org!aero.org!speedy.aero.org!surtsey!don
  3. From: don@surtsey.aero.org (Don C. Hancock)
  4. Subject: Multiple Inheritance Problem
  5. Message-ID: <1992Aug31.230311.4668@speedy.aero.org>
  6. Sender: news@speedy.aero.org
  7. Nntp-Posting-Host: surtsey.aero.org
  8. Organization: The Aerospace Corporation; El Segundo, CA
  9. Date: Mon, 31 Aug 1992 23:03:11 GMT
  10. Lines: 43
  11.  
  12.  
  13. I'm confused as to why the following results in what to me is the wrong
  14. virtual function being called.  I'm sure the problem is somehow caused
  15. by my casting the result of the function call from one base to the
  16. other (and somehow lying to the compiler), but I'm not sure how else to
  17. accomplish my task.
  18.  
  19. Any help would be greatly appreciated.  I've run this on the GNU C++
  20. compiler as well as the Borland compiler with the same results, that
  21. is, the call to derived::v2() ends up calling derived::v1().
  22.  
  23. Don Hancock
  24.  
  25. ----------CUT HERE--------------
  26. #include <stdio.h>
  27.  
  28. class base1 {
  29. public:
  30.     virtual void v1() {printf("base1::v1\n");}
  31. };
  32.  
  33. class base2 {
  34. public:
  35.     virtual void v2() {printf("base2::v2\n");}
  36.  
  37. };
  38.  
  39. class derived : public base1, public base2 {
  40. public:
  41.     virtual void v1() {printf("der::v1\n");}
  42.     virtual void v2() {printf("der::v2\n");}
  43.  
  44. };
  45.  
  46. base1 * fun() {
  47.     derived *dd = new derived;
  48.     return dd;
  49. };
  50.  
  51. main() {
  52.     base2 *bb2 = (base2 *) fun();
  53.     bb2->v2(); // prints 'der::v1'  !!!!!????
  54. }
  55.