使用map& lt; const字符串,function& lt ;?(?)& gt;命令; [英] Parsing commands using map<const string, function<? (?)>> commands;
问题描述
问题陈述:
给出交互式输入,在适当的对象(银行,BankRegister,BasicAccount等)上调用适当的方法.
Given the interactive input, call the appropriate methods on appropriate objects (Bank, BankRegister, BasicAccount, etc...).
输入(每个命令在单独的行上):
input (each command on a separate line):
create_bank <bank_name>\n
create_customer <bank_number> <customer_name>\n
create_account <bank_number> <customer_number>\n
deposit <amount> <account_number>/<bank_number>\n
etc...
建议的解决方案:
#include <functional>
bool create_bank(const string& arguments){
const string& bankName = arguments; //no need to parse here
bankRegister.registerBank(new Bank(bankName,
&bankRegister)); //can't access bankRegister!
return true;
}
int main(){
map<string, function<bool (string)>> commands;
commands.emplace("create_bank",create_bank);
...
BankRegister bankRegister;
string command, arguments;
cin>>command;
getline(cin, arguments);
commands[command](arguments);
...
}
为什么它不起作用:
- 不能通过函数访问bankRegister对象.我可能会通过对函数的常量引用来传递它,但只有某些函数需要它.我当时正在考虑使其成员(m_banks,m_next_bankNumber)成为静态成员,但随后我必须将其公开,这可能是一个错误的设计决定.
- 参数解析应该在哪里进行?这样可以吗?
-
返回值应代表什么?
- The bankRegister object can't be accessed from the functions. I might pass it by constant reference to the functions, but only some functions need it. I was thinking of making its members (m_banks, m_next_bankNumber) static, but then I would have to make them public, which is probably a bad design decision.
- Where should the argument parsing happen? Is it OK this way?
What should the return value represent?
a)参数解析成功
b)参数成功解析并且命令成功执行
b) arguments parsed successfully and command executed successfully
对于某些错误,我使用了异常,但是除了cerr之外,有些是无声的.
I use the exceptions for some of the errors, but some are silent except for cerr.
我可以在这里使用可变参数函数来解析参数吗?
Could I use variadic functions here to parse the arguments?
推荐答案
您可以更改 create_bank
函数的签名,并使用 std :: bind
绑定 BankRegister
参数,使用调用签名 bool(const string&)
You could change the signature of the create_bank
function and use std::bind
to bind the BankRegister
argument, creating a function object with the call signature bool(const string&)
bool create_bank(const string& arguments, BankRegister& bankRegister){
const string& bankName = arguments; //no need to parse here
bankRegister.registerBank(new Bank(bankName,
&bankRegister));
return true;
}
int main(){
BankRegister bankRegister;
map<string, function<bool (string)>> commands;
using std::placeholders::_1;
commands.emplace("create_bank",std::bind(create_bank, _1, std::ref(bankRegister)));
您可以使用lambda函数执行相同的操作:
You can do the same thing with a lambda function:
commands.emplace("create_bank", [&bankRegister](const string& args) { return create_bank(args, bankRegister); });
bind
返回的函数对象和lambda表达式创建的闭包对象都具有必需的调用签名 bool(const string&)
,但它们还包含对 bankRegister
对象的引用,并将其传递给 create_bank
The function object returned by bind
and the closure object created by the lambda expression both have the required call signature, bool(const string&)
, but they also contain a reference to the bankRegister
object and can pass it to create_bank
这篇关于使用map& lt; const字符串,function& lt ;?(?)& gt;命令;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!