Connecting Tech Pros Worldwide Help | Site Map

pointer to virtual function of a base class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:57 PM
Steffen Jahn
Guest
 
Posts: n/a
Default pointer to virtual function of a base class

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, 05:57 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default 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, 05:57 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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, 05:59 PM
Steffen Jahn
Guest
 
Posts: n/a
Default 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, 06:00 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.