c++ - 获取函数指针和类指针的函数?

标签 c++ class function-pointers

我想在一个名为 Map 的类中创建 MapIf 函数。 MapIf 将这样调用:

void addThree(int& n) {
    n += 3;
}

class startsWith {
    char val;

public:

    startsWith(char v) : val(v) {};

    bool operator()(const std::string& str) {
        return str.length() && char(str[0]) == val;
    }
};

int main(){
...
    startsWith startWithB('B');

    Map<std::string, int> msi;

    MapIf(msi, startWithB, addThree);
    return 0;
}

MapIf 的声明是什么?

void MapIf(const Map& map, class condition, void (*function)(ValueType));

这样可以吗?

最佳答案

以下应符合您的原型(prototype)。

template <typename Key, typename Value, typename Condition>
void MapIf(const Map<Key, Value>& map, Condition condition, void (*function)(Value&));

关于c++ - 获取函数指针和类指针的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21090947/

相关文章:

perl - 在 HTML::Element 的 Look_down 例程 Perl 中指定多个类?

C++ - 使用 Visual Studio 的成员函数声明

c++ - 删除不同线程中的对象会导致崩溃?

c++ - 如何使 std::pop_heap 接受适当的函数指针作为比较?

c - 将函数指针作为参数传递时出错

c++ - 更改OpenCV卡尔曼滤波器的增益以使其响应更快

c++ - TabCtrl_GetItem 宏未按预期工作

c++ - 模板类成员函数的模板特化

c++ - 指向可变函数模板的指针

c++ - 理解 C++ 中的命令