Connecting Tech Pros Worldwide Help | Site Map

pointer to virtual function of a base class

  #1  
Old July 19th, 2005, 06:57 PM
Steffen Jahn
Guest
 
Posts: n/a
Hi,

I'm looking for a way to get the pointer to a virtual function of
a base class. That is, I want to force an object to call the
virtual function of a base class. A direct call is no problem, but
I can't find a way to get the right function pointer.

Below is a simple example to describe my problem. -- It's only here
to illustrate the problem. My real implementation uses callback
templates. -- Help is highly appreciated.

Thank you!
Steffen

#include <iostream>

using std::endl ;
using std::cerr ;

struct B {
virtual void f() const { cerr << "B" << endl ; }
} ;

struct C : public B {
virtual void f() const { cerr << "C" << endl ; }
} ;

int main(int argc, char *argv[])
{
C *c = new C ;
c->B::f() ; // direct call: prints "B"
void (B::*g)() const = &B::f ;
(c->*g)() ; // prints "C", but I want "B", so how to set up g?
return 0 ;
}
  #2  
Old July 19th, 2005, 06:57 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a

re: pointer to virtual function of a base class




Steffen Jahn wrote:[color=blue]
>
> Hi,
>[/color]
[color=blue]
> int main(int argc, char *argv[])
> {
> C *c = new C ;
> c->B::f() ; // direct call: prints "B"
> void (B::*g)() const = &B::f ;
> (c->*g)() ; // prints "C", but I want "B", so how to set up g?[/color]

c is still a pointer to a C object. Thus the virtual call
is dispatched to class C.
If you want the function from class B, then you have to make c
into a B pointer:

C* c = new C;
B* b = c;

...

(b->*g)();

Or of course you could use a cast to do the very same.


--
Karl Heinz Buchegger
kbuchegg@gascad.at
  #3  
Old July 19th, 2005, 06:57 PM
Ron Natalie
Guest
 
Posts: n/a

re: pointer to virtual function of a base class



"Steffen Jahn" <stjahn@gmx.de> wrote in message news:a5f85bbf.0309180844.1da4d49c@posting.google.c om...
[color=blue]
> I'm looking for a way to get the pointer to a virtual function of
> a base class.[/color]

No, doesn't matter how you form the pointer to member, the
virtual function overriding is applied after the member pointer
is evaluated.


  #4  
Old July 19th, 2005, 06:59 PM
Steffen Jahn
Guest
 
Posts: n/a

re: pointer to virtual function of a base class


> No, doesn't matter how you form the pointer to member, the[color=blue]
> virtual function overriding is applied after the member pointer
> is evaluated.[/color]

That seems to be right. And I can follow this because it's simply
an offset into the vtable.

Anyways, since the compiler can resolve "c->B::f()" in the desired
way, there might be also a way to get the the address (pointer) of
this function. Anyone has an idea?

Thanks
Steffen
  #5  
Old July 19th, 2005, 07:00 PM
Ron Natalie
Guest
 
Posts: n/a

re: pointer to virtual function of a base class



"Steffen Jahn" <stjahn@gmx.de> wrote in message news:a5f85bbf.0309190052.159e21b@posting.google.co m...

[color=blue]
> Anyways, since the compiler can resolve "c->B::f()" in the desired
> way, there might be also a way to get the the address (pointer) of
> this function. Anyone has an idea?[/color]

NO. I told you before. It is not possible (at least not portably).
The standard doesn't tell you how the code gets to the actual
executable instructions, just that it happens after the member
function pointer is dereferenced.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question related to virtual function babu answers 9 May 18th, 2007 03:55 PM
Virtual function behaviour Rahul K answers 1 May 29th, 2006 08:25 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin answers 12 April 7th, 2006 10:15 AM
calling virtual function results in calling function of base class... Andreas Lagemann answers 8 July 23rd, 2005 12:34 AM