Hello Randy!
On Friday, May 8, 2015 at 9:33:27 AM UTC-4,
mad...@acm.org wrote:
> On Thursday, May 7, 2015 at 6:53:29 PM UTC-4, Norman J. Goldstein wrote:
> > Hi. K. Frank.
> ...
> So with pimpl you get run-time "hot switching" by virtue of being able to
> reassign the internal pointer to refer to a different implementation.
>
> How is that very different from having a pointer to a base class and
> reassigning that to point to a different derived object? Would you
> not consider that to be "hot switching" as well?
You and Norman raise some interesting issues. (Hi Norman!
I see what you mean by "hot switching" now.)
I think reassigning a polymorphic pointer is similar to
hot switching with a pimpl class, but not entirely the same.
As I understand it, if your class has no state (e.g., it
has member functions, but no data member variables), then
reassigning the polymorphic pointer should count as hot
switching (but, in effect, you're really only reassigning
a function pointer).
If your class has state, then (unadorned) polymorphism
won't let you hot switch because if you were to reassign
the polymorphic pointer, you would lose the state of the
pointed-to class.
You don't get around this issue for free with pimpl, but
perhaps it lets you package the machinery better.
With pimpl in its simplest form, the pointer-to-implementation
points to the entire implementation -- both state and the
member functions of the implementation class. If you have
state and reassign the pointer-to-implementation, you will
also lose the state.
So, in this regard, you're right. Reassigning a polymorphic
pointer is pretty much the same as reassigning the
pointer-to-implementation.
But if you elaborate pimpl a little, for example, by having
both a pointer to implemented state (member variables) and a
pointer to implemented behavior (member functions), you
could hot switch behavior by reassigning just the latter
without losing state.
Of course, if you add some "stuff" to the polymorphic-pointer
approach, you could also keep state while hot switching,
but maybe pimpl lets you encapsulate this more cleanly.
One way of looking at this is that -- from the perspective
of the client code -- the pimpl class has (can have), as
Norman mentioned, value semantics, so it can have state that
survives hot switching (provided, of course, that the author
of the pimpl class has implemented the machinery that gives
the pimpl class proper values semantics and/or manages state).
> Randy.
Best regards!
K. Frank