Re: Fun with member-function pointers
Default User wrote:
"Marcel M?ller" <news.5.maazl@spamgourmet.com> wrote in message
news:4e5351c3$0$6566$9b4e6d93@newsspool3.arcor-online.net...
This is undefined behavior.
I think that I'm convinced that you're correct. The example I had built
without issue and gave me the results I wanted, but as we know UB can do
that. Right up until someone changes something and then it doesn't.
To illustrate the point: change your code to (see below) and it will sadly
fail.
You are correct, although adding a virtual destructor to the base class
makes it "work" again.
Use multiple inheritance and it will fail again.
Member function pointers are not the solution of your problem. You should
use an observer pattern instead.
I'm not sure how that pattern would apply to this situation. Perhaps I'll
start a new topic on the more general question.
The base class should provide an event (or something like that) where
derived classes could register a handler. The handler should handle the
operations. It's your choice whether the dispatch table is in the base
(i.e. one handler for each command) or if the dispatch is in the derived
class (i.e. each handler catches the operations he can handle and the
framework tries all handlers until one does the work or it finally fails
- like a window message procedure).
Marcel