正确的静态类方法指针数组的语法

时间:2016-01-08 19:10:12

标签: c++ pointers

我是函数指针的新手,我在语法上挂了。我要做的是在一个类中定义一个函数数组来进行字符串匹配。匹配函数及其存储数组将是静态的,因为它们将由类的所有实例共享。函数存储在一个数组中,因此我可以在match()中迭代并尝试不同的函数。另外,我正在尝试全局地键入dede函数指针,因为类似的匹配函数将在许多这样的类中使用。我发现一些东西暗示签名应该是bool(Money :: FP)(char str)但是,如果是真的,我无法全局定义(即类除了“钱”)?

下面的代码没有编译,所以请将其视为我想要完成的伪代码。

Money.h:

typedef bool(*FP)(char* str);

class Money
{

private:
    static FP matchers[3] = {
        Money::m1,
        Money::m2,
        Money::m3
    };

    static bool m1(char* str);
    static bool m2(char* str);
    static bool m3(char* str);

public:
    static void match(char* str);
};

1 个答案:

答案 0 :(得分:2)

它不起作用,因为null在其声明中引用了Money类型。尝试将它们分离,例如

{
  "153470": {
    "topics": {
      "en": ""
    },
    "classtype_id": "CW",
    "learning_outcomes": {
      "en": ""
    },
    "course_id": "06-DRSOLI0",
    "course_name": {
      "en": "Distributed operating systems"
    },
    "id": 153470,
    "teaching_methods": {
      "en": ""
    }
  },
    "153471": {
    "topics": {
      "en": ""
    },
    "classtype_id": "CW",
    "learning_outcomes": {
      "en": ""
    },
    "course_id": "06-DPROLI0",
    "course_name": {
      "en": "Team project"
    },
    "id": 153471,
    "teaching_methods": {
      "en": ""
    }
  },
    "178903": null,
}

在任何情况下,您可能会考虑使用Money::m1而不是函数指针,因为您正在使用C ++。在您证明性能问题之前,性能不是问题。