"Sommer Bernhard (SMS SW SEC 88T external)" <Mo***********@infineon.com>
wrote in message news:c0**********@newssrv.muc.infineon.com...
"christopher diggins" <cd******@users.sourceforge.net> schrieb im
Newsbeitrag news:E8********************@weber.videotron.net... Hi everyone,
I have the following code (greatly simplified to demonstrate issue) :
#include <iostream>
class CBaseFuBar {
public:
virtual void Fu() { std::cout << "base"; };
};
class CDerivedFuBar : public CBaseFuBar {
public :
virtual void Fu() { std::cout << "derived"; };
};
typedef void (CBaseFuBar::*FuFxnPtr)();
int main() {
CDerivedFuBar fubar;
FuFxnPtr fxn;
fxn = &CBaseFuBar::Fu;
at this point your expression will be solved by compiler and the compiler
will always use the address of function CBaseFuBar::Fu.
The only possible way I see is to wrap your virtual funktion in a non
virtual baseclass function. Something like this:
class CBaseFuBar {
public:
virtual void Fu() { std::cout << "base"; };
void WrapIt(){Fu();}
};
now you can use your wrapper function:
int main() {
CDerivedFuBar fubar;
FuFxnPtr fxn;
fxn = &CBaseFuBar::WrapIt();
(fubar.*fxn)(); // should work
std::cin.get();
return 0;
}
regards
Bernhard
Thanks for the input. I mentioned in the original post that CBaseFuBar could
not be modified, though that can be remedied easily if we place the wrapper
in the derived class :
class CDerivedFuBar {
virtual void Fu() { std::cout << "derived"; };
void WrapFu() { CBaseFuBar::Fu(); };
};
etc.
This is a working solution but not a very practical one unfortunately. I
would have to write a new wrapper function in every derived class for every
virtual function. That would be a heck of a nasty requirement to put on
users who derive from CBaseFuBar. You can imagine the redundancy as my
project scales up. I believe though that this is in fact the best we can do
with the limitations of the current standard. I am just hoping someone
proves us wrong. `
--
Christopher Diggins
yet another language designer
http://www.heron-language.com