English 中文(简体)
通过成员职能点,提出论据并加以储存
原标题:Pass member function pointers by argument and store them
  • 时间:2011-01-24 14:28:11
  •  标签:
  • c++
  • oop

我试图用一个微薄的地雷项目来处理排雷问题。 我试图建立一个简单的门乌班(固定班),以建造、展示和执行一个菜单。 这一菜单应当从“主”方案的第二个班级日历中履行职能。

哪些接口 我努力做到:

int main()
{
    Menu menu;
    menu.add("Exit program", &Calendar::exit);
    menu.show();
    menu.execute(menu.EXIT); // or just 0
}

我们应该看一下:

中东和北非

0 出境方案

我想实现的是,添加(a)-方法应当增加一个功能点,用于绘制地图和设计;int,功能点-点; 这是我的问题开始的。 目前,我有一个硬编码阵列,在我的执行(a)-方法中设有职能点。 现在,我要充满活力地增加一个具有适当职能的菜单项目。

class Menu {
    // declaration of function-pointer thingy
    typedef void (Calendar::*CalendarPtr)(); 

    // will hold the string in the add()-method
    std::vec纽约总部r<std::string> options_strings; 
    // will hold the integer associated 纽约总部 the appropriate function
    std::map<int, CalendarPtr> options_map; 
public:
    // possible options
    enum OPTIONS {EXIT}; 
    // shows the menu on-screen
    void show(); 
    // should add string 纽约总部 vec纽约总部r, function-pointer 纽约总部 map
    void add(std::string, CalendarPtr); 
    // should execute appropriate function (based on option passed)
    void execute(OPTIONS option); 
};

// ...

void Menu::add(std::string option, CalendarPtr) // what doesn t work obviously
{
    // adding string 纽约总部 vec纽约总部r
    options_strings.push_back(option);

    // Just binding the latest entry of the string 纽约总部 the function-pointer
    int index = options_strings.size();

    // TRYING 纽约总部 add function-pointer 纽约总部 map
    options_map.insert(std::pair<int, CalendarPtr>(index, CalendarPtr)); 
}

Menu::execute(OPTIONS option) // enum declared as public in class
{
    int option = EXIT; // or 0
    CalendarPtr cptr[] = {&Calendar::exit};
    Calendar* cal;
    if (option >= 0 && option < static_cast<int>(options_strings.size()))
        (cal->*cptr[option])();
}

插入线目前提供:

页: 1

which means that my use of function-pointers isn t right, but what is the right way? How would I declare the wanted add()-method (so which parameters), how would I insert the function-pointers in the map and how would I call the desired item in the map with my execute()-method?

如果情况不明确,请说,我试图更好/更动地解释:

提前感谢!

http://www.ohchr.org。

void Menu::add(std::string option, CalendarPtr)
// ...
options_map.insert(std::pair<int, CalendarPtr>(index, CalendarPtr)); 

纽约总部

void Menu::add(std::string option, CalendarPtr cptr)
// ...
options_map.insert(std::pair<int, CalendarPtr>(index, cptr)); 

但是,在插入的职位上,你如何“召唤”职能(分析指数0)? 选择:图0;t工作......

<><><><>>>>

它插入了指数1的职称;问题得到解决!

I在我掌握这一方法时有错误:

void Menu::execute(OPTIONS option)
{
    Calendar c;
    Calendar* calendar = &c;
    CalendarPtr cptr = options_map.at(0);
    (*calendar.*cptr)();
}

在扔 instance子后,要求终止:离去——包 what什么():地图:at

因此,我的结论是,这段话没有成功,为什么如此?

最佳回答

您的新增职能如下:

void Menu::add(std::string option, CalendarPtr ptr) // what doesn t work obviously
{
    // adding string to vector
    options_strings.push_back(option);

    // Just binding the latest entry of the string to the function-pointer
    int index = options_strings.size();

    // TRYING to add function-pointer to map
    options_map.insert(std::pair<int, CalendarPtr>(index, ptr)); 
}

我建议设立一个代表(例如。 由此可见的C++代表。 通过成员职能点发出的职能呼吁非常缓慢,代表版本将更加简便,而且非常快。

Update

首先:你实际上没有使用<代码>options_strings,你知道: 你们可以公正使用

std::map<std::string, CalendarPtr> options_map; 

a. 向成员职能点绘制插图。

Update 2:

因此,一旦你恢复<条码>。

void CallIndex(Calendar* calendar, int index) {
    CalendarPtr pfunc = options_map[index].second;
    (*calendar).*pfunc();
}

但是,你可能希望,避免使用协调人-to-member-Function

问题回答

在<代码>Menu:add上,请在<代码>CalendarPtr上注明姓名。 这是合法的,只要你实际上不想利用这一论点!

主要错误是,你试图在<条码><>>>>>>上添加一个类别而不是标的。 另一些则包括——使用“<代码>未加提及”:在参数上载,造成不必要的复制。 如果使用<条码>int来代表一个大小,其容量是不够的,你必须使用<条码>,即。 这里应该帮助:

#include <map>
#include <vector>
#include <string>

struct Calendar {
    void exit () {}
};

class Menu {
    // declaration of function-pointer thingy
    typedef void (Calendar::*CalendarPtr)(); 

    // will hold the string in the add()-method
    std::vector<std::string> options_strings; 
    // will hold the integer associated to the appropriate function
    std::map<size_t, CalendarPtr> options_map; 

public:
    // possible options
    enum OPTIONS {EXIT}; 

    // shows the menu on-screen
    void show(); 

    // should add string to vector, function-pointer to map
    void add(const std::string &, CalendarPtr); 

    // should execute appropriate function (based on option passed)
    void execute(OPTIONS option); 
};

// ...

void Menu::add (const std::string & option, CalendarPtr v) // what doesn t work obviously
{
    // adding string to vector
    options_strings.push_back (option);

    // Just binding the latest entry of the string to the function-pointer
    size_t index = options_strings.size ();

    // TRYING to add function-pointer to map
    options_map.insert (std::make_pair (index, v)); 
}

void Menu::execute(OPTIONS option) // enum declared as public in class
{
    Calendar c;
    option = EXIT; // or 0
    CalendarPtr cptr[] = {&Calendar::exit};
    Calendar* cal = &c;
    if (option >= 0 && option < static_cast<int>(options_strings.size()))
        (cal->*cptr[option])();
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签