我有一個類對象和類型定義:如何在實例創建時將實例本身傳遞給外部映射?
class Object
{
private:
long int id;
public:
Object(void);
~Object(void) {};
long int get_id(void);
};
typedef map<long int, Object> obj_map;
然後,我有這個類應用程序,它有一個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')|
如何傳遞每個對象的實例本身的外部地圖在其創建的時刻?
哦,多麼愚蠢的我。血色指針無處不在! Python已經破壞了我的大腦。我看到這是一個指針本身(對象)。 –