Re: Problem with virtual member function pointer (how to invoke the base?!)

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 02/13/04


Date: Fri, 13 Feb 2004 05:02:27 GMT


"christopher diggins" <cdiggins@users.sourceforge.net> wrote...
> I have the following code (greatly simplified to demonstrate issue) :
>
> #include <iostream>
>
> class CBaseFuBar {
> public:
> virtual void Fu() { std::cout << "base"; };

'virtual' is key here.

> };
>
> class CDerivedFuBar : public CBaseFuBar {
> public :
> virtual void Fu() { std::cout << "derived"; };
> };
>
> typedef void (CBaseFuBar::*FuFxnPtr)();
>
> int main() {
> CDerivedFuBar fubar;
> FuFxnPtr fxn;
> fxn = &CBaseFuBar::Fu;
> (fubar.*fxn)(); // prints out "derived" not "base" like I want

The function is called polymorphically. For all intents and purposes
it works as if you called

    fubar.Fu()

> std::cin.get();
> return 0;
> };
   ^
This semicolon is a syntax error.

>
> The problem is that given CDerivedFuBar I want to get a member function
> pointer to refer to the base Fu function and not automatically get
resolved
> to the derived Fu function. The CBaseFuBar class can not be modified, but
> the CDerivedFuBar class can be. Thanks in advance!

Don't use a pointer to member, use direct call

    fubar.CBaseFuBar::Fu();

Victor