gpt4 book ai didi

c++ - 多个静态类成员函数都具有相同的参数并返回

转载 作者:行者123 更新时间:2023-11-28 03:34:51 24 4
gpt4 key购买 nike

在 C++ 中,假设我有以下头文件:

class Foo {
static int func0 (int, int);
static int func1 (int, int);
static int func2 (int, int);
static int func3 (int, int);
};

有没有办法通过 typedef 做到这一点?

我试过:

class Foo {
typedef int(*func)(int, int);
static func func1;
static func func2;
static func func3;
static func func4;
};

然后在一个cpp文件中

int Foo::func1(int a, int b) { return a + b; }

但是我得到了错误:

Redefinition of func1 as different kind of symbol

最佳答案

func1 已声明为函数指针,而不是您试图将其定义为的函数。

函数指针的使用示例:

typedef int (*func_t)(int, int);

int func1(int a, int b) { return a + b; }
int func2(int a, int b) { return a * b; }

funct_t op = func1;
assert(9 == op(4, 5));

op = func2;
assert(20 == op(4, 5));

话虽如此,我不确定您的确切意图是什么。

关于c++ - 多个静态类成员函数都具有相同的参数并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11241230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com