calling virtual function that is hidden by inheritance

  • Thread starter Kris Thielemans
  • Start date
K

Kris Thielemans

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
 
M

Mike Wahler

Kris Thielemans said:
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
 
A

Ali R.

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

Rob Williscroft

Kris Thielemans wrote in
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.
 
T

tom_usenet

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
 
K

Kris Thielemans

Mike Wahler said:

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
 
R

Ron Natalie

Kris Thielemans said:
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.
 
K

Kris Thielemans

Ron Natalie said:
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
 
K

Kris Thielemans

tom_usenet said:
On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"

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
 
T

tom_usenet

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
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top