pointers - C++ copy constructor issue with parent/child classes -


i've run problem copy constructors...i assume there basic answer , i'm missing obvious - maybe i'm doing entirely wrong - haven't been able figure out.

basically, have parent class , child class. parent class contains vector of pointers (different) base class object. child class wants instead store pointers objects derived base object.

here's pseudocode sample, if helps:

// base classes class itemrev {   ... }  class item {  protected:  vector<itemrev *> m_revptrvec;  }   item::item(const item &inputitemobj) {   // copy contents of input object's item rev pointer vector   vector<itemrev *>::const_iterator veciter = (inputitemobj.m_revptrvec).begin();   while (veciter != (inputitemobj.m_revptrvec).end()) {     (this->m_revptrvec).push_back(new itemrev(**veciter));   } }  =========  // derived classes class jdi_itemrev : public itemrev {   ... }  class jdi_item : public item {  ...  }  jdi_item::jdi_item(const jdi_item &itemobj) {   // copy contents of input object's item rev pointer vector   vector<itemrev *>::const_iterator veciter = (inputitemobj.m_revobjptvec).begin();    // below not work!   while (veciter != (inputitemobj.m_revobjptvec).end()) {     m_revobjptvec.push_back(new jdi_itemrev(**veciter));   } } 

the problem above in push_back() call in jdi_item copy constructor.

given setup, should child class's copy constructor like? need child class copy constructor? assumed did, because copy constructor creating new objects, , parent copy constructor create new objects not type want in derived class (i.e., parent object stores pointers itemrev objects, while child object should store pointers derived jdi_itemrev objects).

as mentioned in comments, there more succinct way express problem (i.e. class structure needs work).

however, if want way, easiest way achieve use virtual clone() method in base class of itemrev, overrides of defined in derived classes.

e.g.:

class itemrev {    virtual itemrev* clone() const = 0; };  class jdi_itemrev : public itemrev {    itemrev* clone() const override    {     // actual cloning here, using copy constructor     return new itemrev(*this);   } }; 

now, whenever call clone() on class derived itemrev, returned itemrev* point constructed derived class. can of course derived class's interface static_cast<> or dynamic_cast<>.

...however...

derivation seems easy win turns out not be. inheritance should used if derived class really a type of base class. people select inheritance when derived class a lot a base class, or shares many characteristics with base class. not time use inheritance. it's time use encapsulation.

in general, inheritance evil.

on note, might find link interesting.

presentation on inheritance implementation detail


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -