Getting rid of dynamic polymorphism and classes

Tommi tommitissari at hotmail.com
Thu Nov 8 11:01:39 PST 2012


On Thursday, 8 November 2012 at 17:50:48 UTC, 
DypthroposTheImposter wrote:
> That example would crash hard if those stack allocated shapes 
> were not in scope...
>
> Making it work safely would probably require std::shared_ptr 
> usage

But the correct implementation depends on the required ownership 
semantics. I guess with Canvas and Shapes, you'd expect the 
canvas to own the shapes that are passed to it. But imagine if, 
instead of Canvas and Shape, you have Game and Player. The game 
needs to pass messages to all kinds of different types of 
players, but game doesn't *own* the players. In that case, if a 
game passes a message to a player who's not in scope anymore, 
then that's a bug in the code that *uses* game, and not in the 
implementation of game. So, if Canvas isn't supposed to own those 
Shapes, then the above implementation of Canvas is *not* buggy.



More information about the Digitalmars-d mailing list