c++ - Base class using data from derived class -


i have class

class {     uint8_t* queue;     uint32_t num_elems_queue;     void dosomethingwithqueue(); }; 

this class library used different clients. in client code used above class have like.

class b {     const uint32_t num_elems = 8;     uint8_t queue[num_elems]; }; 

now in order initialize data in class have 2 options. 1 pass data in class in constructor such

class {     a(uint8_t* queue__, uint32_t num_elems_queue__): queue(queue__),num_elems_queue(num_elems_queue__)     {     }     uint8_t* queue;     uint32_t num_elems_queue;     void dosomethingwithqueue(); }; 

the other inherit class b class , have pure virtual functions in class return pointer needed data. such as

class {      uint8_t* queue;     uint32_t num_elems_queue;     void dosomethingwithqueue();     virtual uint32_t get_num_elems_queue() = 0;     virtual uint8_t* get_queue() = 0; };  class b : public {     const uint32_t num_elems = 8;     uint8_t queue[num_elems];     uint32_t get_num_elems_queue()     {         return num_elems;     }     uint8_t* get_queue()      {         return queue     } }; 

now question preferred way. have been told second method better opinion in case of second method inheriting not extend base class send data base class. not go against basic reason why inheritance should used "to extend functionality of class".

edited: think situation can solved passing "num_elems" class a. in general when have single class works on different data better inherit , use data derived class. correct me if wrong. after little searching found similar example msdn website: code in vb not vb developer understood scenario same. https://msdn.microsoft.com/en-us/library/27db6csx%28v=vs.90%29.aspx

suppose, example, have large business application manages several kinds of in-memory lists. 1 in-memory copy of customer database, read in database @ beginning of session speed. data structure might following:

class customerinfo     protected previouscustomer customerinfo     protected nextcustomer customerinfo     public id integer      public fullname string       public sub insertcustomer(byval fullname string)         ' insert code add customerinfo item list.      end sub       public sub deletecustomer()         ' insert code remove customerinfo item list.      end sub       public function getnextcustomer() customerinfo         ' insert code next customerinfo item list.          return nextcustomer     end function       public function getprevcustomer() customerinfo         'insert code previous customerinfo item list.          return previouscustomer     end function  end class 

your application may have similar list of products user has added shopping cart list, shown in following code fragment:

class shoppingcartitem     protected previousitem shoppingcartitem     protected nextitem shoppingcartitem     public productcode integer      public function getnextitem() shoppingcartitem         ' insert code next shoppingcartitem list.          return nextitem     end function  end class 

you can see pattern here: 2 lists behave same way (insertions, deletions, , retrievals) operate on different data types. maintaining 2 code bases perform same functions not efficient. efficient solution factor out list management own class, , inherit class different data types:

class listitem     protected previousitem listitem     protected nextitem listitem     public function getnextitem() listitem         ' insert code next item in list.          return nextitem     end function      public sub insertnextitem()         ' insert code add item list.      end sub       public sub deletenextitem()         ' insert code remove item list.      end sub       public function getprevitem() listitem         'insert code previous item list.          return previousitem     end function  end class 

the listitem class needs debugged once. can build classes use without ever having think list management again. example:

class customerinfo     inherits listitem     public id integer      public fullname string  end class  class shoppingcartitem     inherits listitem     public productcode integer  end class 

it usualy superior option derive class b a

for example:

class vehicle { public:     int wheelcount; };  class truck : public vehicle { public:     int ladderheight; }; 

a truck vehicle. therefore use derivation. property wheelcount available in truck.

derivation in general means, is thing. in example, truck vehicle. can monstertruck truck (i.e. multiple derivance).

despite fact use std::vector or std::list that, don't think question here. thought worth mentioning...


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -