如何在创建实例时将实例本身传递给外部地图?
How to pass the instance itself to an external map at the moment of its creation?
我有一个 class 对象和一个 typedef:
class Object
{
private:
long int id;
public:
Object(void);
~Object(void) {};
long int get_id(void);
};
typedef map<long int, Object> obj_map;
然后我有这个 class 应用程序,它有一个 obj_map 和 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);
};
如您所见,我们的想法是将应用程序中的每个对象都存储在地图中的 id 下。但是,我希望在创建每个对象的那一刻发生。所以这是构造函数定义:
Object::Object()
{
App::object_counter += 1;
this->id = App::object_counter;
App::objects[this->id] = this; // Problem.
}
错误:
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')|
如何在创建对象时将每个对象的实例本身传递给外部映射?
因为这是一个指针类型 --> 试试 *this
您想在地图中存储副本还是只存储对对象的引用?
如果您的对象具有值语义,则只需分配 *this
(对象)而不是 this
。
另一方面,如果身份很重要,那么构建一个到 Object * 的映射(或者,更好的 std::shared_ptr),然后分配将按原样工作
我有一个 class 对象和一个 typedef:
class Object
{
private:
long int id;
public:
Object(void);
~Object(void) {};
long int get_id(void);
};
typedef map<long int, Object> obj_map;
然后我有这个 class 应用程序,它有一个 obj_map 和 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);
};
如您所见,我们的想法是将应用程序中的每个对象都存储在地图中的 id 下。但是,我希望在创建每个对象的那一刻发生。所以这是构造函数定义:
Object::Object()
{
App::object_counter += 1;
this->id = App::object_counter;
App::objects[this->id] = this; // Problem.
}
错误:
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')|
如何在创建对象时将每个对象的实例本身传递给外部映射?
因为这是一个指针类型 --> 试试 *this
您想在地图中存储副本还是只存储对对象的引用?
如果您的对象具有值语义,则只需分配 *this
(对象)而不是 this
。
另一方面,如果身份很重要,那么构建一个到 Object * 的映射(或者,更好的 std::shared_ptr),然后分配将按原样工作