home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!elroy.jpl.nasa.gov!news.aero.org!aero.org!speedy.aero.org!surtsey!don
- From: don@surtsey.aero.org (Don C. Hancock)
- Subject: Multiple Inheritance Problem
- Message-ID: <1992Aug31.230311.4668@speedy.aero.org>
- Sender: news@speedy.aero.org
- Nntp-Posting-Host: surtsey.aero.org
- Organization: The Aerospace Corporation; El Segundo, CA
- Date: Mon, 31 Aug 1992 23:03:11 GMT
- Lines: 43
-
-
- I'm confused as to why the following results in what to me is the wrong
- virtual function being called. I'm sure the problem is somehow caused
- by my casting the result of the function call from one base to the
- other (and somehow lying to the compiler), but I'm not sure how else to
- accomplish my task.
-
- Any help would be greatly appreciated. I've run this on the GNU C++
- compiler as well as the Borland compiler with the same results, that
- is, the call to derived::v2() ends up calling derived::v1().
-
- Don Hancock
-
- ----------CUT HERE--------------
- #include <stdio.h>
-
- class base1 {
- public:
- virtual void v1() {printf("base1::v1\n");}
- };
-
- class base2 {
- public:
- virtual void v2() {printf("base2::v2\n");}
-
- };
-
- class derived : public base1, public base2 {
- public:
- virtual void v1() {printf("der::v1\n");}
- virtual void v2() {printf("der::v2\n");}
-
- };
-
- base1 * fun() {
- derived *dd = new derived;
- return dd;
- };
-
- main() {
- base2 *bb2 = (base2 *) fun();
- bb2->v2(); // prints 'der::v1' !!!!!????
- }
-