C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Calling base class virtual method

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Raider
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Calling base class virtual method Reply with quote



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?
Back to top
Ivan Vecerina
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Re: Calling base class virtual method Reply with quote



"Raider" <sraider (AT) yandex (DOT) ru> wrote in message
news:1158741166.883383.263580 (AT) m7g2000cwm (DOT) googlegroups.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() Sad
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
Back to top
Raider
Guest





PostPosted: Wed Sep 20, 2006 9:10 am    Post subject: Re: Calling base class virtual method Reply with quote



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

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

But it calls Derived::Show() Sad
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.