Getting polymorphism to work in a C++ map without memory leaks? -


it's been long3 time since programmed in c++. polymorphism isn't working: map<string, base> converts armybase , navybase objects base objects when add them map, getdescription() returns empty string rather values set via armybase::setdescription() , navybase::setdescription(). here's extremely rough pseudo-code:

class base { protected:     string str__description;  // pardon non-standard style   public:     virtual     string getdescription()     { return str__description;     }     void   setdescription( string str__description )     { str__description = str__description;     } }  class armybase: public base { public:     string getdescription()     { return str__description + " (army)";     } }  class navybase: public base { public:     string getdescription()     { return str__description + " (navy)";     } } 

it sounds map<string, base*> causes memory leaks , i'd rather not upgrade mid-project use shared_ptr. would storing derived-class instances in container "destructs" them allow me use pointer map polymorphism without risk of memory leakage?

base                         base; armybase                     base_army; set<armybase>                set__armybases; map<string, base*>::iterator iter_bases; map<string, base*>           map__bases; navybase                     base_navy; set<navybase>                set__navybases; ... while( ... ) {   base_army = armybase();     base_navy = navybase();     ...     set__armybases.insert(                                        base_army   );     map__bases.insert(     pair<string, base*>( "boca raton",    &base_army ) );     ...     set__navybases.insert(                                        base_navy   );     map__bases.insert(     pair<string> base*>( "nas pensacola", &base_navy ) );     ...     base = iter_bases->second;     std::cout << ..." " << base->getdescription() << std::endl; } 

desired output map__bases:

boca raton ... (army) nas pensacola ... (navy) ... 

the problem map entries point adresses of objects created on stack. copy objects sets, don't store addresses of copies in maps. when original objects fall out of scope, deleted automatically , map entries become invalid.

i think best way solve problem allocate objects on heap , store pointers in containers. this, of course, requires careful memory management. know 3 options handle this:

  1. manual memory management: delete objects when erase them container. dangerous , error-prone, of course, care, can make work. wrapping container in class manage objects, i.e., wrapper has methods such add(base* base) , remove(base* base), , delete objects in container in destructor. of course, must still take care not delete such managed objects outside of wrapper.

  2. smart pointers: use either shared_ptr or unique_ptr (depending on ownership semantics) , store in container. smart pointers take care of deleting objects when removed container.

  3. use custom allocator. can parametrize std containers allocators should allow container delete objects when removed map. however, have never done , can't comment on whether it's idea. hear writing custom allocators quite difficult right.

i suggest either 1 , 2. think depends on taste , on other requirements 1 use, if use smart pointer option, make sure choose smart pointer models particular ownership semantics (most unique_pointer).


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -