Topic: How to use scope resoultion operator with pointer to membersr ?
Author: p150650@zori.cs.tut.fi (Tero Pulkkinen)
Date: 1996/10/25 Raw View
[Note: This discussion isn't really related to C++ standardization:
approving the original article for comp.std.c++ was probably a
mistake. Followups have been redirected to comp.lang.c++. mha]
> void B::f()
> {
> A::f();
> }
>
> Unfortunately, this won't work with pointers (at least on my compiler).
There's nothing you cannot do with A::f(); thingy. Every virtual function
call through a pointer or reference picks the function in the leaf of the
inheritance hierarchy.
> So I can't
> do the following (note: B is derived from A, f is a virtual method):
>
> void B::f()
> {
> (((A *)this)->*mptr)(); // Compiles ok, but results in infinite
> recursion!
> // OR
> A::(((A *)this)->*mptr)(); // Dosen't compile!
> // Where mptr is of type void (A::* )() and set to &A::f;
> }
Why would you want to do this?
Once you handle pointers of that type of objects, you dont necessary know
the exact type of the object(1*) and each virtual function call should work
as they do in that example, thus resulting to infinite recursion.
If you know the exact type of the object, you can *always* use A::f();
(only such place is in one of the member functions of the object)
1* Because you can set a pointer of derived class type to base class ptr,
it'd result an faulty operation if you'd give wrong type object there,
and you'd lose the advantages of static type checking.
It is always enforced by the C++ compiler that if a virtual function is
called, the derived class function is called instead of base class function.
And your way of doing it would break that enforcement.
>
> Anyone got a solution to this problem ?
>
Make another function(nonvirtual) to your base class, copy default
implementation of the virtual function to that nonvirtual function and
replace the default implementation of virtual function with just a call
to the nonvirtual function. That way, you have two different ways to call
the function and only one way to call it can be replaced in derived classes.
(And it shows in the public interface of the object :)
All the followups goes to comp.lang.c++. (i hope :)
--
-- Tero Pulkkinen -- terop@modeemi.cs.tut.fi --
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: "Morten M. Christensen" <mmc@dit.ou.dk>
Date: 1996/10/23 Raw View
Hi,
I have problems using the scope resoultion operator :: with pointer to
members (operator X::*). The problem is that I have a superclass
A with a virtual method A::fxxxx which has been redefined in a derived
class B and that I need to call A::fxxx using a pointer to
member-function
from B.
When I am not using pointers to members (that is: When I know the exact
member
function to call), I can do something like this from B::f to call A::f :
void B::f()
{
A::f();
}
Unfortunately, this won't work with pointers (at least on my compiler).
So I can't
do the following (note: B is derived from A, f is a virtual method):
void B::f()
{
(((A *)this)->*mptr)(); // Compiles ok, but results in infinite
recursion!
// OR
A::(((A *)this)->*mptr)(); // Dosen't compile!
// Where mptr is of type void (A::* )() and set to &A::f;
}
Anyone got a solution to this problem ?
Thanks,
Morten M. Christensen
Odense Lindoe, Denmark
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]