473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 7360

"Kris Thielemans" <kr*************@imperial.ac.uk> wrote in message
news:6c**************************@posting.google.c om...
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.uk> wrote in message
news:6c**************************@posting.google.c om...
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.google.c om:

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******@mkwahler.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******@mkwahler.net> wrote in message news:<WO****************@newsread3.news.pas.earthl ink.net>...
"Kris Thielemans" <kr*************@imperial.ac.uk> wrote in message
news:6c**************************@posting.google.c om...
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.uk> wrote in message news:6c**************************@posting.google.c om...
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.newshosting.c om>...
"Kris Thielemans" <kr*************@imperial.ac.uk> wrote in message news:6c**************************@posting.google.c om...
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********@hotmail.com> wrote in message news:<bc********************************@4ax.com>. ..
On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
<mk******@mkwahler.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********@hotmail.com> wrote in message news:<bc********************************@4ax.com>. ..
On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
<mk******@mkwahler.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
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
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...
4
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...
5
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...
3
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...
3
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...
12
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...
23
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
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;"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.