简体   繁体   中英

#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?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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