RE: [Dev-C++] Help with creating objects dynamically
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: xRxIxPx <xr...@gm...> - 2003-07-22 08:30:50
|
At 18:40 2003-07-21 +0200, xRxIxPx wrote:
>At 16:36 2003-07-21 +0530, Somanathan Kaimal wrote:
>
>#include <map>
>#include <string>
>
>#include "MyObjectClass.h"
>//and then have you object database
>
>std::map<std::string, MyObjectClass*> obj_db;
>
>void add_to_db()
>{
> MyObjectClass* obj = new MyObjectClass;
> obj_db.insert(std::map<std::string, MyObjectClass*>::value_type(
> std::string(getName()), obj));
>}
>
>MyObjectClass& get_from_db(const std::string& name)
>{
> std::map<std::string, MyObjectClass*>::iterator it;
> it = obj_db.find(name);
> if (it == map.end()) throw std::string("name not found int obj_db");
>
> return *it;
>}
D`oh. s/return *it/return **it/
And deletion:
void delete_from_db(const std::string& name)
{
std::map<std::string, MyObjectClass*>::iterator it;
it = obj_db.find(name);
if (it == map.end()) throw std::string("name not found int obj_db");
delete *it;
obj_db.erase(it);
}
|