【发布时间】:2015-05-26 00:58:23
【问题描述】:
我有一系列非常相似的成员函数,我认为我可以使用模板或其他方法使我的代码更易于维护,但我不知道该怎么做。
这是我的一个函数的示例:
void CalController::bgc_cmd(const std::string& s) {
try {
this->cohort_ptr->md->set_bgcmodule(temutil::onoffstr2bool(s));
LOG(note) << "CalController turned bgc module to "
<< s <<" via cohort pointer...";
} catch (const std::runtime_error& e) {
LOG(warn) << e.what();
}
}
我的其他功能是相同的,除了:
- 函数名称(即
bgc_cmd(..)、env_cmd(..)、dsl_cmd(..) - 在
try...catch块中调用的成员函数(属于md类)
基本上我想避免在我的每个CalController::XXX_cmd(...) 函数中重复try..catch 块和LOG(..) 消息。
使用boost::function 和或boost::bind 就可以了,我只是在兜圈子,不知道如何设置它。
【问题讨论】:
标签: c++ templates boost member-functions