Re: Function pointer help
On Oct 24, 8:32 pm, James Kanze <james.ka...@gmail.com> wrote:
On Oct 24, 6:57 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Oct 24, 2:50 pm, James Kanze <james.ka...@gmail.com> wrote:
[]
IMHO, using member function pointers for callbacks is
always a design mistake because it requires casting member
function pointers, which is not portable.
There's no problem with portabilty, as long as the user
doesn't make any mistakes. There's a serious problem with
robustness, however, since you really have no way of
catching those mistakes.
Although it works on practice, is it not undefined behaviour
to cast a derived class member function pointer back to base
class member function pointer?
No. Undefined practice only creeps in if you use it with a
pointer that doesn't actually point to a derived class of the
correct type. (I had the same reaction the first time I saw
it:-). It doesn't like like it should work, especially if e.g.
virtual inheritance et al. are involved. But the standard says
it has to.)
[]
3) they do not rely on any undefined behaviour.
Nor does the pointer to member solution.
Sorry, I was wrong with regards to UB arising when casting derived
class member function pointer to base class one. It seems that
static_cast<> is enough to do the trick.
--
Max