Path: utzoo!attcan!uunet!ginosko!gem.mps.ohio-state.edu!wuarchive!psuvax1!rutgers!dptg!ulysses!andante!alice!shopiro From: shopiro@alice.UUCP (Jonathan Shopiro) Newsgroups: comp.lang.c++ Subject: Re: MI and virtual functions Summary: You always get the same foo() Keywords: multiple inheritance, virtual functions, ambiguity Message-ID: <9981@alice.UUCP> Date: 27 Sep 89 21:02:31 GMT References: <2534@fai.UUCP> Organization: AT&T Bell Laboratories, Murray Hill NJ Lines: 47 In article <2534@fai.UUCP>, kurtl@fai.UUCP (Kurt Luoto) writes: > I have been reading Lippman's "C++ Primer" to familiarize myself with > the features of C++ 2.0. On page 312, in discussing function name > ambiguities that can arise from multiple inheritance, he gives an example > and states: > > "When the inherited members that share the same name are all > member functions, it is therefore a good design strategy to > define a derived class member function of the same name." In fact, if they are virtual functions, you _must_ declare a function by that name in the derived class. > > I wanted to try this out myself and see how it worked with virtual > functions. I do not yet have AT&T C++ 2.0 available to me, but I do > have GNU C++. Reversing the order of the base classes has no effect on the output of your program in AT&T C++ 2.0. As a simple example of how the language should behave: struct A { virtual void foo(); }; struct B { virtual void foo(); }; struct C : A, B { virtual void foo(); }; void bar() { C c; A* ap = &c; B* bp = &c; C* cp = &c; // these calls all do the same thing ap->foo(); // calls C::foo() bp->foo(); // calls C::foo() cp->foo(); // calls C::foo() } -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229