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

Calling base class virtual method

P: n/a
I have class hierarchy with virtual methods like this:

class Base
{
public:
virtual void Show();
~Base();
};

class Derived : public Base
{
public:
virtual void Show();
void DoDerivedStuff();
~Base();
};

And I have a function dealing with Derived objects:

void foo(Derived& d)
{
d.DoDerivedStuff();
d.Show(); // <- but here I want to call Base::Show(),
// not Derived::Show()
// (or even DerivedDerived::Show())
}

What should I write to call Base::Show() in foo()?

Should I write special adaptor like this
void Derived::BaseShow()
{
Base::Show();
}
and call it? Or may be there is a simpler way?

Sep 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What should I write to call Base::Show() in foo()?

I tried pointer to member function:
(d.*(&Base::Show))();

But it calls Derived::Show() :-(

Sep 20 '06 #2

P: n/a
"Raider" <sr*****@yandex.ruwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
:What should I write to call Base::Show() in foo()?
:
: I tried pointer to member function:
: (d.*(&Base::Show))();
:
: But it calls Derived::Show() :-(
Close - but you must avoid using a function pointer for it to work:
d.Base::Show(); // shall do
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Sep 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.