Re: Function pointer help
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.)
And looks wrong because there are easier ways to achieve
the desired effect of calling back a member function of an
object. It is trivial to make it right in a 100% portable
way using C-style callbacks, i.e. function pointer + void*
pointer. Such callbacks can be bound to regular functions
as well as to member functions. The only cast required is
absolutely safe static_cast<T*>(void*):
The code you propose is no safer than the pointer to member
solution with my template wrapper.
I think it is, because it does not rely on casting member
function pointers.
It requires using a void*, which in my experience can be even
more dangerous.
And a lot uglier and more difficult to understand.
It looks alien to you probably because you are not used to it.
Certainly:-). (Actually, I can follow it, because when
interfacing to C, you often have to do things like that. It is
the first time I've seen it used when there was no C involved,
however.)
I think what I really don't like about it is that it doesn't
look wierd enough, given what you're actually doing.
[...]
Speaking purely technically, I think C-style callbacks are
superior to member function pointer based callbacks:
Quite frankly, neither come close to using virtual functions.
However...
1) they can be bound both to plain functions and to member functions.
If that is a requirement, of course (e.g. to be able to
interface with C as well), then your solution is the way to go.
2) the callback structure occupies less memory, since a
regular function pointer is 2-3 times smaller than a member
function pointer.
As if that's seriously going to make any difference. This is a
GUI we're talking about. With large, graphic images behind the
scenes; a couple of Meg, at the least. And even with a hundred
callbacks, you won't be adding over a KB.
3) they do not rely on any undefined behaviour.
Nor does the pointer to member solution.
--
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