Tuesday 15 June 2010

c++ - Clarification about Sean Parent's talk "Inheritance is the base class of evil" -



c++ - Clarification about Sean Parent's talk "Inheritance is the base class of evil" -

sean parent's talk, inheritance base of operations class of evil, says polymorphism not property of type, rather property of how used. thumb rule, don't utilize inheritance implement interfaces. among many benefits of devirtualization of classes have virtual functions because implementing interface. here's illustration :

class drawable { public: virtual void draw() = 0; }; class drawa : public drawable { public: void draw() override{//do something} }; class usedrawable { public: void do(){mdraw->draw();} drawable* mdraw; };

here, instead of usedrawable requiring mdraw drawable*, have utilize type-erased class can wrap around class implementing fellow member called draw. so, boost::type_erasure::any appropriate definition. way, drawa doesn't need inherit drawable - polymorphism usedrawables requirement , not property of drawa.

i trying refactor code next principle. have abstract class modelinterface , 2 concrete classes modela , modelb inheriting modelinterface. next sean's advice, makes sense not forcefulness modela , modelb inheritance hierarchy , utilize type-erasure @ locations require class satisfying concept modelled modelinterface.

now, problem places in code utilize modelinterface constructing appropriate object based on runtime configuration file. currently, mill new appropriate object , homecoming modelinterface*. if refactor code utilize type-erased concept(say boost::type_erasure::any<implement modelinterface>) @ these locations in code, how build such objects @ runtime? modela , modelb still need rtti-enabled classes? or can factory-construct , utilize them without rtti info somehow?

(with rtti, can have abstract class, factoryconstructible, , utilize dynamic_cast<void*> final type.)

type erasure 101:

step 1: create regular (or semi-regular move-only) type hides detail.

this class exposes concepts want support. copy, move, destroy, equals, total order, hash, and/or whatever custom concepts need support.

many of these concepts can mapped pure virtual interface method in current inheritance based solution.

create non-virtual methods in regular type expresses concepts. copy/assign copy, etc.

step 2: write type erasure helper. here have pure virtual interfaces. clone() copy,etc.

store smart pointer1 in regular type above. forwards regular methods helper.

step 3: write type erasure implementation. template class stores t , inherits helper, , forwards interface t. utilize free functions (sort of std::begin) uses methods in default implementation if no adl free function found.

step 4: add together constructor regular type takes t , constructs type erasure implementation it, , stuffs in smart pointer helper.

after work, have non-intrusive polymorphic scheme regular (or semi-regular) value type.

your mill functions homecoming regular type.

look sample implementations of std::function see done concetely.

1 both unique , shared choices, depending on if want store immutable/copy on write data, or manually clone.

c++ inheritance c++11 factory type-erasure

No comments:

Post a Comment