473,769 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling virtual function that is hidden by inheritance

Hi

I have a rather outlandish problem where I want to call a virtual
function which is (sort of) hidden by a derived class. For instance

class A { virtual void func(); };
class B: public A { virtual void func(); }

void some_routine()
{ B b;
b.func(); // will call B::func
??? // how to call A::func() for the b object
}

I know how to do this within the definition of a member. For instance
void B::func()
{
A::func();
}

but I need it outside the class.

In case you wonder why I need it... I am writing a test program. In my
case A::func() and B::func() should give the same results, but
B::func() does it faster. My test program wants to check if the
results are indeed the same.

Thanks for any help!

Kris
Jul 19 '05 #1
9 7385

"Kris Thielemans" <kr************ *@imperial.ac.u k> wrote in message
news:6c******** *************** ***@posting.goo gle.com...
Hi

I have a rather outlandish problem where I want to call a virtual
function which is (sort of) hidden by a derived class. For instance

class A { virtual void func(); };
class B: public A { virtual void func(); }

void some_routine()
{ B b;
b.func(); // will call B::func
??? // how to call A::func() for the b object
}

I know how to do this within the definition of a member. For instance
void B::func()
{
A::func();
}

but I need it outside the class.

In case you wonder why I need it... I am writing a test program. In my
case A::func() and B::func() should give the same results, but
B::func() does it faster. My test program wants to check if the
results are indeed the same.


#include <iostream>

class A
{
public:
virtual void func() { std::cout << "A\n"; }
};

class B: public A
{
public:
virtual void func() { std::cout << "B\n"; }
};

int main()
{
B b;
b.func(); /* prints "B" */
static_cast<A>( b).func(); /* prints "A" */
return 0;
}

-Mike
Jul 19 '05 #2
Hi Kris,

Mike's way is one way. Fortunately this is more than one way to skin the
cat. I like this one better :
what you do is use the name of the parent class just like a member variable
followed by ::

class Parent
{
public:
virtual void func() { std::cout << "A\n"; }
};

class Child : public Parent
{
public:
virtual void func() { std::cout << "B\n"; }
};

int main()
{
Child b;

b.func(); /* prints "B" */
b.Parent::func( ); /* prints "A" */

return 0;
}
Ali R.

"Kris Thielemans" <kr************ *@imperial.ac.u k> wrote in message
news:6c******** *************** ***@posting.goo gle.com...
Hi

I have a rather outlandish problem where I want to call a virtual
function which is (sort of) hidden by a derived class. For instance

class A { virtual void func(); };
class B: public A { virtual void func(); }

void some_routine()
{ B b;
b.func(); // will call B::func
??? // how to call A::func() for the b object
}

I know how to do this within the definition of a member. For instance
void B::func()
{
A::func();
}

but I need it outside the class.

In case you wonder why I need it... I am writing a test program. In my
case A::func() and B::func() should give the same results, but
B::func() does it faster. My test program wants to check if the
results are indeed the same.

Thanks for any help!

Kris

Jul 19 '05 #3
Kris Thielemans wrote in
news:6c******** *************** ***@posting.goo gle.com:

void some_routine()
{ B b;
b.func(); // will call B::func
??? // how to call A::func() for the b object
b.A::func();
}


HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:
int main()
{
B b;
b.func(); /* prints "B" */
static_cast<A>( b).func(); /* prints "A" */


Did you mean to copy the object?

static_cast<A&> (b).func();
or just
b.A::func();

Tom
Jul 19 '05 #5
"Mike Wahler" <mk******@mkwah ler.net> wrote in message news:<WO******* *********@newsr ead3.news.pas.e arthlink.net>.. .
"Kris Thielemans" <kr************ *@imperial.ac.u k> wrote in message
news:6c******** *************** ***@posting.goo gle.com...
Thanks Mike

however it seems that your solution doesn't work for me (using gcc
3.2). The reason being that my base class A has other pure (and
unimplemented) virtual members. When I do the static_cast<A> trick I
get an error "cannot allocate an object of type A because the
following virutal functions are abstract ..."

In contrast, the suggestion by Ali and Rob (to use b.A::func()) works
fine in that case as well.

Thanks all 3 of you.

Kris
class A
{
public:
virtual void func() { std::cout << "A\n"; }
};

class B: public A
{
public:
virtual void func() { std::cout << "B\n"; }
};

int main()
{
B b;
b.func(); /* prints "B" */
static_cast<A>( b).func(); /* prints "A" */
return 0;
}

-Mike

Jul 19 '05 #6

"Kris Thielemans" <kr************ *@imperial.ac.u k> wrote in message news:6c******** *************** ***@posting.goo gle.com...
class A { virtual void func(); };
class B: public A { virtual void func(); }

void some_routine()
{ B b;
b.func(); // will call B::func
??? // how to call A::func() for the b object
}


You can't do this. That's the whole point of access control.
You can't access A's private parts. It's got nothing whatsoever
to do with virtual or hiding.
Jul 19 '05 #7
"Ron Natalie" <ro*@sensor.com > wrote in message news:<3f******* *************** *@news.newshost ing.com>...
"Kris Thielemans" <kr************ *@imperial.ac.u k> wrote in message news:6c******** *************** ***@posting.goo gle.com...
class A { virtual void func(); };
class B: public A { virtual void func(); }


You can't do this. That's the whole point of access control.
You can't access A's private parts. It's got nothing whatsoever
to do with virtual or hiding.


oops. sorry. forgot the public there! You're right of course. but the
other posters gave me the answer I wanted anyway. lucky me.

thanks

kris
Jul 19 '05 #8
tom_usenet <to********@hot mail.com> wrote in message news:<bc******* *************** **********@4ax. com>...
On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:

static_cast<A&> (b).func();
right, that would solve my pure virtual problem I mentioned.

However, this static_cast trick actually does not work. It will still
call B::func(). I checked this with gcc, but I find the easiest
explanation as follows:

In my opinion, the above is functionally identical to

A& a_ref = b; /* feel free to insert a static_cast here, but you
don't have to*/
a_ref.func();

Now, I hope you'll agree that the last statement definitely calls
B::func(). Otherwise there would be no point in having virtual
functions. Indeed suppose you have a function that works on A&
objects, which version of func() do you want it to call normally?

void some_other_func (A& a)
{
a.func();
}
some_other_func (b); // will call B::func

or just
b.A::func();


this works fine.

Kris
Jul 19 '05 #9
On 9 Oct 2003 06:44:06 -0700, kr************* @imperial.ac.uk (Kris
Thielemans) wrote:
tom_usenet <to********@hot mail.com> wrote in message news:<bc******* *************** **********@4ax. com>...
On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
<mk******@mkwah ler.net> wrote:

static_cast<A&> (b).func();


right, that would solve my pure virtual problem I mentioned.

However, this static_cast trick actually does not work. It will still
call B::func().


Whoops, of course, since func is virtual (I hadn't spotted that).

Tom
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2225
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
4
2901
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
4
1738
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final question remaining: How to properly, simple and eleganty implement functions. The functions are encapsulated in the objects, the objects themselves are stored in a binary tree and objects contain 'methods' (functions) along with data. I don't want to...
5
2614
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
3
1714
by: Imre | last post by:
Hi! I've got some questions regarding heavy use of virtual inheritance. First, let's see a theoretical situation, where I might feel tempted to use a lot of virtual inheritance. Let's suppose, we're creating a little strategy game. In our game, there are Units. A Unit can be either a Human, or a Vehicle. Obviously, Human and Vehicle are subclasses of Unit.
3
4551
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
12
2663
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
23
4614
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
17
3547
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9979
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9849
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8861
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5433
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.