It's been a long3 time since I programmed in C++. My polymorphism isn't working: the map<string, Base> converts my ArmyBase and NavyBase objects to Base objects when I add them to the map, so GetDescription() returns an empty string rather than the values I set via ArmyBase::SetDescription() and NavyBase::SetDescription(). Here's the extremely rough pseudo-code:
class Base
{ protected:
string STR__Description; // Pardon my 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 like map<string, Base*> causes memory leaks and I'd rather not upgrade mid-project to use shared_ptr. Would storing the derived-class instances in a container that "destructs" them properly allow me to use the pointer map for 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 from map__Bases:
Boca Raton ... (Army)
NAS Pensacola ... (Navy)
...