Re: Address of virtual member function from object (*not* class)
On Sep 2, 7:02 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
James Kanze wrote:
On Sep 2, 4:07 pm, Gabriel de Dietrich <gabriel.dedietr...@gmail.com>
wrote:
On Sep 2, 3:28 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
"Curiosity" is not really a valid reason, I believe. So, really, why
would you want to do that? You're not using the pointer-to-member in
any way, so what would be the point of returning it?
Well, it is for me... :-)
To answer your question: Suppose you have an arbitrarily large
inheritance hierarchy with root class A, and you want to know
if some subclass reimplements f. If you know the class, no
problem. Just test
&A::f == &B::f
or whatever other class, and it's done.
If I read the standard correctly, that test shoud always return
true (supposing that f is virtual, and that B derives from A).
Regardless of whether B overrides the function or not. (I'm not
100% sure about my reading of the standard here, however, since
both of the compilers available to me say that the comparison is
illegal.)
Pointers to members respect virtuality; a pointer to a virtual
function will "point" to the correct function in whatever object
it happens to be used with.
Yes, but, again, how would he get a pointer to member from a
pointer to an object (possibly of the derived type)?
You can't. Generally speaking, pointer to member is a static
concept (since pointers to members exist in the absense of any
actual object).
Would 'typeid' help?
Not the standard typeid, anyway. An implementation could extend
it so that it did (say by adding a function to std::type_info
which returned a reference to a _Class---an implementation
defined class along the lines of Java's java.lang.Class), but I
don't know of any which do. (As far as I know, the demand for
it is quite low. When I used Java, except for a few limited
uses which are easily implemented differently---and more
safely---in C++, the only use I found for it was for some
generic test programs of data containers.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34