c++ - Thoughts on different types of inheritance -


in looking @ following simple code make sense introduce virtual destructor if know not deleting base pointer? seems should try avoid vtable ups if possible performance reasons. understand premature optimization etc. question in general. wondering thoughts on following:

  • using protected destructor if not deleting items through base pointer
  • the overhead associated introducing single virtual method

also, if class has destructor virtual method lookup overhead destructor method , other methods not incur penalty or once introduce vptr suffers? assuming each class have vptr inside of have perform vptr lookups on destructor.

class cardplayer {   public:     typedef std::vector<cardplayer> collectiontype;      explicit cardplayer()=default;      explicit cardplayer(const card::collectiontype& cards);     explicit cardplayer(card::collectiontype&& cards);      void receivecard(const card& card);      bool discardcard(card&& card);     void foldcards();      inline const card::collectiontype& getcards()  { return cards_; }     // virtual ~cardplayer() = default;  // should introduce vtable if not needed?   protected:     ~cardplayer()=default;     card::collectiontype cards_; }; -------------------------------------------------------------------- #include "cardplayer.h" #include <functional>  class blackjackplayer : public cardplayer {   public:     typedef std::vector<blackjackplayer> collectiontype;     typedef std::function<bool(const card::collectiontype&)> hitfntype;    blackjackplayer(hitfntype fn) : hitfn_(fn) {}    bool wanthit()    {     return hitfn_(getcards());   }    hitfntype hitfn_; }; 

i'd avoid virtual destructor , hence adding vtbl class in case. can protect class being deleted through base class pointer, seems that, without having other virtual methods, premature pessimisation :)

also, having 1 more pointer per instance (the vtbl) can add in large projects. performance depending on memory access , should keep object size small possible , memory access patterns local possible. vtbl in different memory location , in worst case, ask processor read cache line delete object.

to answer other question had: virtual methods routed via vtbl, non-virtual calls uneffected.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -