Calling base class virtual method

R

Raider

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?
 
R

Raider

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

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

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

Ivan Vecerina

:> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top