Topic: Pointer to member function conversion


Author: "Paul Micke" <Paul.Micke@mch.sni.de>
Date: 1998/06/09
Raw View
Is an explicit cast from a pointer to member function of a derived class to
a pointer to member function of a base class legal C++?

Look at the example:

class Base {
};

typedef void (Base::*CallbackType)();

class Timer {
 Base * m_obj;
 CallBackType m_f;
public:
 Timer(CallBackType f, Base *obj) : m_f(f), m_obj(obj) {}

 void call() { m_obj->(*m_f)(); }
};

class Derived: public Base
{
 Timer m_timer;

 void callback() {}
public:
 Derived() : m_timer((CallBackType)callback, this) {}

 void call() { m_timer.call(); }
};

// End example

Explanation:
I have a Timer class which expects a callback member function pointer as
well as a pointer to the corresponding object of type Base.
I derive classes from Base and define different callback functions in the
derived classes. Pointers to these functions are passed in the Derived
class constructor to the constructor of Timer member variables. Since the
type of the pointer to member function of the Timer constructor does not
match exactly I cast the type to the Base class pointer to member function:
 Derived() : m_timer((CallBackType)callback, this) {}
This works with MSVC++ 5.0 and also with another compiler I use in the
project.
It produces the right results, even with virtual callback functions.

But now I have read in the documentation of MSVC++ 5.0 that this cast is
illegal.
And the C++ Standard Draft says that this cast is at least not a valid
standard conversion (it is difficult to read so I am not sure if it says
that this cast is illegal even if it is explicit).

If I write the cast in an assignment statement
 CallBackType pBaseMemberFunction = (CallBackType)Derived::callback;
the MSVC++ 5.0 compiler complains (but the other compiler does not).
If I use the cast in the constructor initializer list of Derived as shown
in the example it works.

Now I am not sure if it is illegal C++ to write such a cast, or if it is an
error of the
MSVC++ compiler to emit an error if I use the cast in an assignment?
In the error message the compiler says that I should use a C-style cast or
reinterpret_cast, but both produce the same error message.
Or simply overlooked the compiler writers the possiblity of such a cast in
the constructor initializer list?

Thanks for any comments on that problem.

Paul



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/06/09
Raw View
Paul Micke <Paul.Micke@mch.sni.de> writes:

> Is an explicit cast from a pointer to member function of a derived class to
> a pointer to member function of a base class legal C++?

Yes, you can do that with static_cast, as it is the reverse of a
standard conversion, and it is expected to work (except if the base
class is a virtual base; in this case, the direct conversion is
ill-formed).

> If I write the cast in an assignment statement
>  CallBackType pBaseMemberFunction = (CallBackType)Derived::callback;

Pointer to members can only be created by using the address-of (&)
operator, so your code is wrong; it should be:

 CallBackType pBaseMemberFunction = (CallBackType)&Derived::callback;
                                                  ^

Perhaps you could rework your example so that callback is declared as
a virtual member function in the base class.  Then, you could refer to
it as &Base::callback, or even stop using pointer to members, as
virtual member function dispatching may be enough.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/06/11
Raw View
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
: Paul Micke <Paul.Micke@mch.sni.de> writes:

: > Is an explicit cast from a pointer to member function of a derived class to
: > a pointer to member function of a base class legal C++?

: Yes, you can do that with static_cast, as it is the reverse of a
: standard conversion,

What happens if the member, the pointer points to, is
not present in the base class?

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/06/11
Raw View
Oleg Zabluda <zabluda@math.psu.edu> writes:

> Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
> : Paul Micke <Paul.Micke@mch.sni.de> writes:

> : > Is an explicit cast from a pointer to member function of a derived class to
> : > a pointer to member function of a base class legal C++?

> : Yes, you can do that with static_cast, as it is the reverse of a
> : standard conversion,

> What happens if the member, the pointer points to, is
> not present in the base class?

Undefined behavior.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]