By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Problem with virtual member function pointer (how to invoke the base?!)

P: n/a
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;
(fubar.*fxn)(); // prints out "derived" not "base" like I want
std::cin.get();
return 0;
};

The problem is that given CDerivedFuBar I want to get a member function
pointer to refer to the base Fu function and not automatically get resolved
to the derived Fu function. The CBaseFuBar class can not be modified, but
the CDerivedFuBar class can be. Thanks in advance!

--
Christopher Diggins
yet another language designer
http://www.heron-language.com
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"christopher diggins" <cd******@users.sourceforge.net> wrote...
I have the following code (greatly simplified to demonstrate issue) :

#include <iostream>

class CBaseFuBar {
public:
virtual void Fu() { std::cout << "base"; };
'virtual' is key here.
};

class CDerivedFuBar : public CBaseFuBar {
public :
virtual void Fu() { std::cout << "derived"; };
};

typedef void (CBaseFuBar::*FuFxnPtr)();

int main() {
CDerivedFuBar fubar;
FuFxnPtr fxn;
fxn = &CBaseFuBar::Fu;
(fubar.*fxn)(); // prints out "derived" not "base" like I want
The function is called polymorphically. For all intents and purposes
it works as if you called

fubar.Fu()
std::cin.get();
return 0;
}; ^
This semicolon is a syntax error.

The problem is that given CDerivedFuBar I want to get a member function
pointer to refer to the base Fu function and not automatically get resolved to the derived Fu function. The CBaseFuBar class can not be modified, but
the CDerivedFuBar class can be. Thanks in advance!


Don't use a pointer to member, use direct call

fubar.CBaseFuBar::Fu();

Victor
Jul 22 '05 #2

P: n/a


"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:DTYWb.293273$I06.3017457@attbi_s01...
"christopher diggins" <cd******@users.sourceforge.net> wrote...
I have the following code (greatly simplified to demonstrate issue) :

#include <iostream>

class CBaseFuBar {
public:
virtual void Fu() { std::cout << "base"; };


'virtual' is key here.
};

class CDerivedFuBar : public CBaseFuBar {
public :
virtual void Fu() { std::cout << "derived"; };
};

typedef void (CBaseFuBar::*FuFxnPtr)();

int main() {
CDerivedFuBar fubar;
FuFxnPtr fxn;
fxn = &CBaseFuBar::Fu;
(fubar.*fxn)(); // prints out "derived" not "base" like I want


The function is called polymorphically. For all intents and purposes
it works as if you called

fubar.Fu()


Yes I realize that.
std::cin.get();
return 0;
};

^
This semicolon is a syntax error.


Thank you for pointing that out.

The problem is that given CDerivedFuBar I want to get a member function
pointer to refer to the base Fu function and not automatically get

resolved
to the derived Fu function. The CBaseFuBar class can not be modified, but the CDerivedFuBar class can be. Thanks in advance!


Don't use a pointer to member, use direct call

fubar.CBaseFuBar::Fu();

Victor


The code I am working with specifically requires a function pointer. Is
there a way to accomplish this with a function pointer invoked on a
CDerivedFuBar object?

Thanks for you input.

--
Christopher Diggins
yet another language designer
http://www.heron-language.com
Jul 22 '05 #3

P: n/a

"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
Jul 22 '05 #4

P: n/a
"Sommer Bernhard (SMS SW SEC 88T external)" <Mo***********@infineon.com>
wrote...
[...]
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();}
};


The OP specifically said that the base class could NOT be modified.

Beyond that, your solution should work.
Jul 22 '05 #5

P: n/a
"christopher diggins" <cd******@users.sourceforge.net> wrote...


"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:DTYWb.293273$I06.3017457@attbi_s01...
"christopher diggins" <cd******@users.sourceforge.net> wrote...
I have the following code (greatly simplified to demonstrate issue) :

#include <iostream>

class CBaseFuBar {
public:
virtual void Fu() { std::cout << "base"; };


'virtual' is key here.
};

class CDerivedFuBar : public CBaseFuBar {
public :
virtual void Fu() { std::cout << "derived"; };
};

typedef void (CBaseFuBar::*FuFxnPtr)();

int main() {
CDerivedFuBar fubar;
FuFxnPtr fxn;
fxn = &CBaseFuBar::Fu;
(fubar.*fxn)(); // prints out "derived" not "base" like I want


The function is called polymorphically. For all intents and purposes
it works as if you called

fubar.Fu()


Yes I realize that.
std::cin.get();
return 0;
};

^
This semicolon is a syntax error.


Thank you for pointing that out.

The problem is that given CDerivedFuBar I want to get a member function pointer to refer to the base Fu function and not automatically get

resolved
to the derived Fu function. The CBaseFuBar class can not be modified, but the CDerivedFuBar class can be. Thanks in advance!


Don't use a pointer to member, use direct call

fubar.CBaseFuBar::Fu();

Victor


The code I am working with specifically requires a function pointer. Is
there a way to accomplish this with a function pointer invoked on a
CDerivedFuBar object?


The only thing I can think of is to drop the 'Fu' member from the derived
class. Then, for any instance, the final overrider will be <base>::Fu,
and it will be called through the pointer.

Victor
Jul 22 '05 #6

P: n/a

"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();

This will fail to compile. It should be fxn = &CBaseFuBar::WrapIt; // NO braces
Also this program will also print derived and not base.

-Sharad
Jul 22 '05 #7

P: n/a
"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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.