Sign in to follow this  

[C++] Take address of virtual function

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some object of some class, which parent classA has virtual function. I want know if this function was overloaded in child class. I try to compare addresses of functions. But when i write such code: if( &ClassA::function != &(object->function) ) compiler says error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&ClassA::function' Google gives a lot of links, but i don't find one, where takes address of function from object. any comments?..

Share this post


Link to post
Share on other sites
AFAIK, There is no formal way for doing this. Why don't you have a "capabilities" function that can easily tell you what your class can or cannot do? Anyway, good solutions are dependant on the exact problem you have. Share it and let's think it out together.

Share this post


Link to post
Share on other sites
Wouldn't it be possible to compare the two objects vptr tables? I've never tried that before...

hth
moe.ron

[EDIT]
Took out some wrong info
[/EDIT]

[Edited by - moeron on June 22, 2005 11:45:00 AM]

Share this post


Link to post
Share on other sites
The thing is the VTBL is not well-defined by the standard. It's compiler-dependent and you are taking a risk if you try guessing its structure (though it's true that most of the compilers behave more or less the same way).

Share this post


Link to post
Share on other sites
I don't think that's possible to do with C++ code.
Do it in asm instead. Generate an asm-listing of some code that calls the function. From that code it's pretty easy to extract the bit that does the vtable lookup

Share this post


Link to post
Share on other sites
Quote:
Original post by moeron
Wouldn't it be possible to compare the two objects vptr tables? I've never tried that before...

hth
moe.ron

[EDIT]
Took out some wrong info
[/EDIT]

You are not supposed to do anything with the vtbl. You are not supposed to know where it is located. You are supposed to use it transparently. Doing anything else is an interesting way to commit suicide.

Quote:
Original post by miptPatriot
I have some object of some class, which parent classA has virtual function. I want know if this function was overloaded in child class.

Teh noes. From an OO design point of view, it breaks the Liskov substitution principle (pdf here). when you use an object that inherits classA you should not have to worry whether the methods has been overloaded or not. If you want to trigger some particuler action, a better way is to add a isOverloaded() virtual function to classA and overload this methods to return either true or false.
Unless you are doing very specific stuff - as specific as the stuff I was doing when I needed to know how to get a raw pointer to a non-virtual member function (I was trying to see if the user modified the code by computing a checksum on an important part of the code and checking the result with the known correct one; the code to get the pointer to the method code is here. it do not work for virtual methods, and it is completely VC6/VC7 centric; if you want to get rid of the asm part, you might use unions - it works better, and do not need asm) - you don't need to find/compare/handle the address of a member function. Just try to find a better, more abstract way to get the job done.

Regards,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Emmanuel makes a good point. What are you really trying to do?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Emmanuel makes a good point. What are you really trying to do?

*ahem*
Quote:
Original post by bytecoder
If you tell us what you're trying to do we can propose a better solution.


Anyway, Emmanuel is right, there's almost always a better way to do something like this.

Share this post


Link to post
Share on other sites

This topic is 4558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this