[C++] Take address of virtual function

Started by
10 comments, last by Shannon Barber 18 years, 10 months ago
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?..
Advertisement
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.
Dubito, Cogito ergo sum.
sounds like you are trying to do reflection in C++ to me...
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
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]
moe.ron
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).
Dubito, Cogito ergo sum.
If you tell us what you're trying to do we can propose a better solution.
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
This site has some good information on what function ptrs to members mean, it has some stuff on VTables and other goodies. Take a look. Linky
moe.ron
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,
Emmanuel makes a good point. What are you really trying to do?

This topic is closed to new replies.

Advertisement