Topic: Pointer to base version of a specialized member function???


Author: rfb@lehman.com (Rick Busdiecker)
Date: Thu, 30 Jun 1994 18:43:21 GMT
Raw View
I had a minor error in my last message, however it does not affect eh output:

In article <RFB.94Jun30142933@fis1510.lehman.com> rfb@lehman.com (Rick Busdiecker) writes:

    . . .

 cout << "d->*df () ==> " << ((d->*bf) ()) << endl;
       ^
         Should be df

I had both calls specifying the base version.  Correcting that doesn't
change the fact that both calls end up invoking the derived version
which is not what I was expecting, i. e., I'd still like to know how
to get the base version of a specialized member function as a
pointer-to-member-function.

--
Rick Busdiecker <rfb@lehman.com>      Please do not send electronic junk mail!
  Lehman Brothers
  388 Greenwich Street      "One never notices what has been done; one can only
  New York, NY 10013         see what remains to be done." -- Marie Curie




Author: rfb@lehman.com (Rick Busdiecker)
Date: Thu, 30 Jun 1994 18:29:33 GMT
Raw View
Given a pointer to an instance of a derived class, you can invoke the base
class version of a virtual member function by explicitly specifying the
class name.  How can you get a pointer-to-member-function that allows you
to do the same thing?  For example, consider this program:

 #include <iostream.h>

 class Base { public: virtual char* f (void) { return "Base::f()"; }};

 class Derived : public Base
 { public: virtual char* f (void) { return "Derived::f()"; }};

 int main (int, char**)
 {
     Derived* d = new Derived;

     cout << "d->f () ==> " << (d->f ()) << endl;
     cout << "d->Derived::f () ==> " << (d->Derived::f ()) << endl;
     cout << "d->Base::f () ==> " << (d->Base::f ()) << endl;

     char* (Base::*bf) (void) = &Base::f;
     char* (Derived::*df) (void) = &Derived::f;

     cout << "d->*df () ==> " << ((d->*bf) ()) << endl;
     cout << "d->*bf () ==> " << ((d->*bf) ()) << endl;
 }

which generates this output:

 d->f () ==> Derived::f()
 d->Derived::f () ==> Derived::f()
 d->Base::f () ==> Base::f()
 d->*df () ==> Derived::f()
 d->*bf () ==> Derived::f()

How do you specify the pointer-to-member-function so that it will invoke
Base::f?  I had expected the program to generate the following output:

 d->f () ==> Derived::f()
 d->Derived::f () ==> Derived::f()
 d->Base::f () ==> Base::f()
 d->*df () ==> Derived::f()
 d->*bf () ==> Base::f()

That is, I expected the last line of the output to be different.

   Rick




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sun, 3 Jul 1994 15:44:49 GMT
Raw View
In article <RFB.94Jun30142933@fis1510.lehman.com> rfb@lehman.com (Rick Busdiecker) writes:
>Given a pointer to an instance of a derived class, you can invoke the base
>class version of a virtual member function by explicitly specifying the
>class name.  How can you get a pointer-to-member-function that allows you
>to do the same thing?

 You cant. For each virtual function, there are TWO
functions -- one is called when a non-virtual call is made,
and the other when a virtual call is made (and it dispatches
to the "final overrider").

 Pointers to members can point to one or the other function.
The language specifies the virtual version. (As far as I know).

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: freja@mip3035.Berkeley.EDU (Etienne Frejaville)
Date: 6 Jul 94 14:40:01 GMT
Raw View
In article <RFB.94Jun30142933@fis1510.lehman.com>, rfb@lehman.com (Rick Busdiecker) writes:
|> Given a pointer to an instance of a derived class, you can invoke the base
|> class version of a virtual member function by explicitly specifying the
|> class name.  How can you get a pointer-to-member-function that allows you
|> to do the same thing?  For example, consider this program:
|>
|>  #include <iostream.h>
|>
|>  class Base { public: virtual char* f (void) { return "Base::f()"; }};
|>
|>  class Derived : public Base
|>  { public: virtual char* f (void) { return "Derived::f()"; }};
|>
|>  int main (int, char**)
|>  {
|>      Derived* d = new Derived;
|>
|>      cout << "d->f () ==> " << (d->f ()) << endl;
|>      cout << "d->Derived::f () ==> " << (d->Derived::f ()) << endl;
|>      cout << "d->Base::f () ==> " << (d->Base::f ()) << endl;
|>
|>      char* (Base::*bf) (void) = &Base::f;
|>      char* (Derived::*df) (void) = &Derived::f;
|>
|>      cout << "d->*df () ==> " << ((d->*bf) ()) << endl;
|>      cout << "d->*bf () ==> " << ((d->*bf) ()) << endl;
|>  }
|>
|> which generates this output:
|>
|>  d->f () ==> Derived::f()
|>  d->Derived::f () ==> Derived::f()
|>  d->Base::f () ==> Base::f()
|>  d->*df () ==> Derived::f()
|>  d->*bf () ==> Derived::f()
|>
|> How do you specify the pointer-to-member-function so that it will invoke
|> Base::f?  I had expected the program to generate the following output:
|>
|>  d->f () ==> Derived::f()
|>  d->Derived::f () ==> Derived::f()
|>  d->Base::f () ==> Base::f()
|>  d->*df () ==> Derived::f()
|>  d->*bf () ==> Base::f()
|>
|> That is, I expected the last line of the output to be different.
|>

To my knowledge, there is no way to do this. The fact of writing :

  char* (Base::*bf) (void) = &Base::f;

i.e taking the address of the virtual Base::f
does not suppress the virtual mechanism when you attempt to call f with :

    (d->*bf) ()

d being a pointer on Derived.

The only way to suppress the virtual mechanism is to qualify explicitely
the DIRECT call (not by means of a pointer to member-function) with the
scope operator (see 10.2) i.e :

  d->Base::f()

as you wrote it.

------Etienne FREJAVILLE--- Bull S.A   ---------------------------------
 OSS/HEU/ASD/TOPAS                      e-mail: E.Frejaville@frcl.bull.fr
 Rue Jean-Jaures, F5-238                tel: (33-1) 30806548
 78340 Les Clayes-sous-Bois, France     Fax: (33-1) 30803338