Pointer to member function conversion
Is conversion from pointer to member function of derived class to
pointer to member function od base class legal?
I think not because of {4.11 Pointer to member conversions
[conv.mem] $2} part of the Standard. However I made a simple test. The
testing code was (in a simpler version) like this:
class base;
typedef void (base::*base_mem_fun_ptr)();
class base {
public:
base(base_mem_fun_ptr p);
};
class derived : public base {
public:
derived();
void mem_fun();
};
derived::derived() : base(&derived::mem_fun) {}
The first thing Ifound out was that this code will not compile on
the compilers I tested (GCC [Cygwin, g++ 3.4.4] VC++ [6.0, 2003,
2005]). However if the pointer was cast using C style cast or
static_cast the code compiled. But:
1)
MS compilers issued warrning (or at least 2003 and 2005 did) that
the code may be generated bad and I was in all three versions. The
"mem_fun" was called with "this" pointing to the "base" subobject of
the "derived" object.
2)
GCC did not issued any warrning and worekd fine. "mem_fun" was
called with "this" pointing to "derived" object not "base" subobject.
Also reinterpret_cast worked to however generted results like in MS
compilers.
What is more I found in the net a site (about GCC):
http://marc.theaimsgroup.com/?l=egcs-bugs&m=108077670204435&w=2
wherewe can read that:
"(...) explicit conversions from a pointer to member function of a
derived class type, to a pointer to member function of a base class
type. The standard explcitly states that such conversions are valid.
(...)".
This made me really confused right now. So how is it? Is this
conversion legal or not?
Adam Badura
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]