简体   繁体   English

#define函数参数c ++

[英]#define function arguments c++

I have a set of functions called in a common interface, and I store those function pointers in a common container, so I have 我在一个公共接口中调用了一组函数,并将这些函数指针存储在一个公共容器中,所以我有

typedef void(*CommandFunction)(const InputArgsMap &, const ArgumentMap *);

With this said, what is the best way to declare functions of this type without copy-pasting the argument list? 有了这个说,如果没有复制粘贴参数列表,声明这种类型的函数的最佳方法是什么? I thought of implementing this via a #define , but is there any other (better, oop) way? 我想通过#define实现这个,但还有其他(更好的,oop)方式吗?

For instance, is it possible to do something like 例如,是否可以做类似的事情

#define CMD_ARGS (const InputArgsMap &, const ArgumentMap *)
void _fcn_1(CMD_ARGS);
void _fnc_2(CMD_ARGS);

If you declare a function, rather than pointer, type alias 如果声明一个函数而不是指针,则键入别名

typedef void CommandFunction(const InputArgsMap &, const ArgumentMap *);

then you can use it to declare functions 然后你可以用它来声明函数

CommandFunction _fcn_1;
CommandFunction _fcn_2;

You'll still need to write out the parameter list when you define them. 在定义参数列表时,您仍需要写出参数列表。

is there any other (better, oop) way? 有没有其他(更好的,oop)的方式?

Overriding a virtual member function of an abstract interface might be nicer, depending on exactly what you're doing. 覆盖抽象接口的虚拟成员函数可能更好,具体取决于您正在做什么。 You will have to duplicate the parameter list if you do that, which you seem to find distasteful; 如果你这样做,你将不得不复制参数列表,你似乎觉得这很令人反感; but in modern C++ you can at least use the override specifier to make sure you get that right. 但是在现代C ++中,你至少可以使用override说明符来确保你做对了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM