I have a class Object and a typedef:
class Object
{
private:
long int id;
public:
Object(void);
~Object(void) {};
long int get_id(void);
};
typedef map<long int, Object> obj_map;
And then I have this class App, which has an obj_map and object_counter:
class App
{
public:
static ALLEGRO_DISPLAY *display;
static ALLEGRO_EVENT_QUEUE *event_queue;
static ALLEGRO_TIMER *timer;
static ALLEGRO_EVENT e;
static bool running;
static bool redraw;
static key_map key_states;
static obj_map objects; // << Here.
static long int object_counter; // << Here.
const char *window_title;
int screen_width;
int screen_height;
float FPS;
act event_scenes;
act visual_scenes;
ALLEGRO_COLOR background_color;
static ALLEGRO_EVENT event();
static ALLEGRO_EVENT_TYPE event_type();
static void shut_down();
App(int screen_width, int screen_height, const char *window_title = "Joy++ Application", float FPS = 30);
~App() {};
int init_all();
void register_all();
void check_key_states();
void init_key_states();
void run();
void destroy_all();
void add_event_scene(Scene scene);
void add_visual_scene(Scene scene);
void remove_event_scene(Scene scene);
void remove_visual_scene(Scene scene);
long int get_object_count();
unsigned int get_random_int(unsigned int min, unsigned int max);
void set_key_state(int al_key, string key_name, bool state);
void set_background_color(int r, int g, int b);
};
As you can see, the idea is to store every object inside the app, under an id, inside a map. But, I want that to happen at the moment of the creation of each object. So here's the constructor definition:
Object::Object()
{
App::object_counter += 1;
this->id = App::object_counter;
App::objects[this->id] = this; // Problem.
}
Error:
G:\Development\Game-Development\CB\Joy-Plus-Plus\app.cpp|26|error: no match for 'operator=' (operand types are 'std::map<long int, Object>::mapped_type {aka Object}' and 'Object* const')|
How can I pass the instance itself of each Object to the external map at the moment of its creation?
If your Object has value semantic then just assign
*this
(the object) and notthis
.On the other hand if identity counts then build a map to Object * (or, better std::shared_ptr) and then the assignment will work as is