Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一组无界的不同名称的方法来匹配一些模式,这些模式可以在C+中根据需要调用+;? 目前我正在从Python转移到C++,我想创建一个无限的函数集,可以根据需要调用: namespace funcs1 { int get_1() {return 1;} int get_2() {return 2;} //... } //could also be class funcs2 { static int get_1() {return 1;} static int get_2() {return 2;} //... } int main() { funcs1::get_123456789(); //return 123456789 funcs2::get_123456789(); //return 123456789 return 0; }_C++ - Fatal编程技术网

如何创建一组无界的不同名称的方法来匹配一些模式,这些模式可以在C+中根据需要调用+;? 目前我正在从Python转移到C++,我想创建一个无限的函数集,可以根据需要调用: namespace funcs1 { int get_1() {return 1;} int get_2() {return 2;} //... } //could also be class funcs2 { static int get_1() {return 1;} static int get_2() {return 2;} //... } int main() { funcs1::get_123456789(); //return 123456789 funcs2::get_123456789(); //return 123456789 return 0; }

如何创建一组无界的不同名称的方法来匹配一些模式,这些模式可以在C+中根据需要调用+;? 目前我正在从Python转移到C++,我想创建一个无限的函数集,可以根据需要调用: namespace funcs1 { int get_1() {return 1;} int get_2() {return 2;} //... } //could also be class funcs2 { static int get_1() {return 1;} static int get_2() {return 2;} //... } int main() { funcs1::get_123456789(); //return 123456789 funcs2::get_123456789(); //return 123456789 return 0; },c++,C++,由于get_x()中的x可能任意大,因此我不能只创建无限数量的函数。在python中,可以通过以下方式完成: 类函数数据(类型): def uu getattr uu(u,键): def(): 返回int(键拆分(“”“)[1]) 返回f 类Funcs(元类=FuncsMeta): 通过 assert funcs.get_123456789()==123456789 我想知道如何在C++中实现这一点:p> 您可以只编写一个函数,它接受一个参数并计算结果: static int getX(int

由于
get_x()
中的
x
可能任意大,因此我不能只创建无限数量的函数。在python中,可以通过以下方式完成:

类函数数据(类型):
def uu getattr uu(u,键):
def():
返回int(键拆分(“”“)[1])
返回f
类Funcs(元类=FuncsMeta):
通过
assert funcs.get_123456789()==123456789

我想知道如何在C++中实现这一点:p> 您可以只编写一个函数,它接受一个参数并计算结果:

static int getX(int input){
    return input;
}
在您的例子中,您只是返回一个似乎与函数名匹配的数字,get部分除外。您可以通过只使用一个函数并在函数体中计算答案,使用输入参数来确定输出结果应该是什么来做类似的事情

这样,您就不必生成大量不必要的函数,这些函数将无法维护且编译速度缓慢

编辑:另一个例子:

static int getX(int input){
    return input + 1;
}

关键是,与创建许多不同名称的方法不同,您只有一个方法,并在“名称”或参数中使用pass来计算如何获得所需的结果。

您不能。您试图解决的实际问题是什么?@yurikilochek我想提供一组函数,e.x.get_x(),供用户调用。我可以从函数名中提取x,进行一些计算,然后返回一些值。我可能会称之为“按函数名传递参数”,而不是按值或按引用传递参数。那么,您可能会放弃使用模板模板int int(){{返回;i }’随后将使用这个“断言(GET())=123456789)。模板是在C++中提供通用的一组事物的灵活方式。@ Mellester很好地了解C++的方式。但是仍然要等待确切的
get_x()
呼叫应答,直到有人告诉我在C++中这是不可能的,
intget(inti){return i;}
?在Python中,将参数
传递给函数
\uu getattr\uuu
,结果是一个整数。为什么它必须是不同的函数,而不是一个带参数的函数?我希望我可以这样做。但我面对的是一组单元测试套件,当她调用
get_x()
时,需要一个
x
。想法是:x可能是20个不同的值,我可以在其中找到一些模式,我不想相应地创建20个不同的函数。@xyshell问题是,如果你能找到一个模式,你就可以编写它,因此,不要创建20个不同的函数,而只创建一个函数。如果你告诉我们你要找的模式是什么,可能很简单。好吧,让我们做一个具体的例子。有一个单元测试套件需要
get_1()==2;获取_2()==4。。。。;get_20()==40你能不能只写一个函数而不是20个函数?@xyshell是的,你能。与其编写这么多不同的函数,不如只编写一个函数并定义输入和输出参数。由于我不知道您使用的是哪种测试框架,所以我无法发表评论,但可维护的方法是按照建议的方式进行。