用于替换回调命名空间的模板?

Template to replace the namespace of a callback ? cpp

本文关键字:命名空间 替换 回调 用于      更新时间:2023-10-16

我有一个名为 Menu 的类。这是它的标头实现。

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();

如您所见,菜单有一个菜单项列表。我认为额外的数据与这个问题无关。

下面是菜单项头文件:

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。

我该如何解决这个问题? 我研究的一个选项是将模板添加到 Menu 和 MenuItem 类中。我想避免这个选项,因为我已经写了一堆代码,诚然有点太愚蠢了(没有提前考虑(,重构会浪费很多时间。这甚至可以用模板实现吗?我可以将模板用作命名空间而不是对象类型吗?

我在这里还有什么其他选择?

你可以使用std::function

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

用户仍然可以将 lambda/Functor 作为

menuItem.setCallback([this](std::string a) { return this->my_callback(a); }