2017-08-10 30 views
0

我有一個名爲Menu的類。這是它的頭部實現。用於替換回調的名稱空間的模板? cpp

class Menu{ 
private: 
    int last_mouse_active_hold_x; 
    int last_mouse_active_hold_y; 

public: 
    float x; 
    float y; 
    float width; 
    float height; 
    int  x_full; 
    int  y_full; 
    int  width_full; 
    int  height_full; 
    bool is_shown; 

    int  num_of_items_shown; 
    int  height_per_item_full; 
    float height_per_item; 
    int  item_offset; 
    int  selected_index; 
    float outside_scroll_speed; 
    int  scroll_speed; 
    int  scroll_counter; 


    std::string  name; 
    unsigned char shortcut; 
    std::string  search_term; 

    MenuItem * items; 
    int   items_len; 
    MenuItem * items_full; 
    int   items_full_len; 

       Menu(); 
    void  render(); 
    void  set_pos(float x, float y); 
    void  set_pos(int x, int y); 
    void  set_width(float w); 
    void  set_name(std::string _name); 
    void  reshape(); 
    void  show(); 
    void  hide(); 
    void  toggle_show(); 
    void  add(std::string _name, int (*_callback)(std::string), std::string _callback_param); 
    void  select(int _index); 
    void  set_number_of_items(int _n); 
    void  set_height(int _h); 
    void  set_width(int _w); 
    void  set_shortcut(unsigned char c); 
    void  mouse_move_passive(int _x, int _y); 
    void  mouse_move_active(int _x, int _y); 
    void  mouse_press(int _button, int _state, int _x, int _y); 
    void  key_press(unsigned char _key, int _x, int _y); 
    void  key_press_special(unsigned char _key, int _x, int _y); 
    void  scroll(int _ammount); 
    void  search(); 
    void  refill_items(); 
    void  pop_item(int _i); 
    void  trigger(); 

正如你所看到的菜單有MenuItems列表。我不認爲額外的數據與這個問題有關。

這裏是菜單項的頭文件:

class MenuItem{ 
public: 
    std::string  name; 
    int    (* callback)(std::string); 
    std::string  callback_param; 
    int    x_full; 
    int    y_full; 
    int    width_full; 
    int    height_full; 
    float   x; 
    float   y; 
    float   width; 
    float   height; 
    float   text_size; 
    float   text_margin_left; 
    float   text_margin_bottom; 
    bool   selected; 

      MenuItem(); 
    void create(std::string _name, int (*callback)(std::string) ); 
    void render(float _x, float _y, float _z); 
    void reshape(); 
    void set_size(int _w, int _h); 
    void set_height(int _h); 
    void set_param(std::string _p); 
    int  trigger(); 

}; 

現在,這是出路,我可以讓我的實際問題。 問題出在MenuItem類的回調函數中。 回調預計類型

int (*callback(std::string a))

的東西這是所有好的和工作正常,如果我給它的功能是主代碼的一部分,或者是一個類中的一個靜態的。但是,我的目標是能夠將此菜單存儲在其他類中,並讓它們將一些自己的方法傳遞給它。這是一個問題,因爲我的大多數類,想使用它,他們通過在類型回調

int (* BaseClass::callback(std::string a))

因此,我得到一個編譯錯誤,說我傳遞一個類型不正確的菜單作爲回調對象。

我該如何解決這個問題?我研究的一個選項是添加模板到Menu和MenuItem類。我想避免這種選擇,因爲我已經寫了一堆代碼,無可否認有點愚蠢(沒有考慮未來),重構會浪費很多時間。這將甚至可能與模板?我可以使用模板作爲命名空間而不是對象類型嗎?

我還有什麼其他選擇嗎?

+0

?如果您使用的是C++ 11或更高版本,則可以使用lambda函數來調用lambda中捕獲的對象的相應成員函數。 – dlasalle

+0

不使用C++ 11。這是一個很好的選擇,但我會在未來記住它。 – suli

+0

公共數據meber幾乎從來都不是一個好主意......並且可能有3/4的功能也應該是私有的。如果你想要一個指向成員函數的指針,然後更新你的代碼...... **重構不是浪費時間**如果你保存了錯誤的代碼,你最終將會有更多的時間在將來修復它。 – Phil1970

回答

1

您可以使用std::function

std::function<int(std::string)> callback; 

用戶還可以通過拉姆達/仿函數作爲

menuItem.setCallback([this](std::string a) { return this->my_callback(a); } 
您正在使用哪種C++標準的版本
+0

我認爲lambda解決方案是cpp11的權利?但我喜歡std :: function解決方案。基本上我會改變我的菜單和MenuItem類,以採取一個std ::功能的權利?這將是回調? – suli

+0

std :: function很好,但是它不起作用,因爲回調是一個重載成員函數,所以我得到一個編譯錯誤。不過,感謝你的回答,我閱讀了lambda函數。謝謝一堆!這個作品 – suli

+0

lambda和std :: function確實來自C++ 11。 – Jarod42

相關問題