我是c ++模板的新手,所以请耐心等待。
我想要做的是通过使用模板化函数在我的班级中实现某种策略模式。我认为这将内联策略。
我的理解是这可以通过仿函数来实现,但我不想引入新的类,我只想在我的班级中内联策略函数。
假设我有一个班级Calculator
。
Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator
{
public:
Calculator();
virtual ~Calculator();
typedef void (*Strategy)(int param1, int param2);
void add(int param1, int param2);
template<class T>
void doStrategy(T strategy, int param1, int param2);
protected:
private:
};
#endif
Calculator.cpp
Calculator::Calculator()
{
//ctor
}
Calculator::~Calculator()
{
//dtor
}
void
Calculator::add(int param1, int param2)
{
std::cout << "Sum " << param1+param2 << std::endl;
}
template<class T>
void
Calculator::doStrategy(T strategy, int param1, int param2)
{
strategy(param1,param2);
}
的main.cpp
int main()
{
Calculator calc = Calculator();
calc.doStrategy<Calulator::Strategy>(calc.add,2,3);
return 0;
}
失败
error: no matching function for call to ‘Calculator::doStrategy(<unresolved overloaded function type>, int, int)’|
note: candidate is:|
note: template<class T> void Calculator::doStrategy(T, int, int)|
note: template argument deduction/substitution failed:|
note: cannot convert ‘calc.Calculator::add’ (type ‘<unresolved overloaded function type>’) to type ‘void (*)(int, int)’|
==稍后编辑==
的main.cpp
typedef void (Calculator::*Strategy)(int, int);
int main()
{
Calculator calc = Calculator();
Strategy strategy = &Calculator::add;
calc.doStrategy<Strategy>(strategy,2,3);
return 0;
}
仍然失败:
undefined reference to `void Calculator::doStrategy<void (Calculator::*)(int, int)>(void (Calculator::*)(int, int), int, int)'
答案 0 :(得分:5)
void add(int param1, int param2)
不是静态方法,因此在对象的实例上调用它。
这意味着它无法转换为typedef void (*Strategy)(int param1, int param2)
,这是一个采用2个整数并且不返回任何内容的方法,因为前add
具有隐含在代码中的隐式this
但存在于现实中。实际上,该方法的签名是void (Calculator::*)(int,int)
。只需将方法设置为static
,就可以了。
我建议您阅读指向成员函数的指针如何详细工作here,但由于您使用的是C ++,我建议您充分利用仿函数。
答案 1 :(得分:0)
我的英语很短......只需保留代码......
在calculator.h中
// this template function use only for calculator's method
// and method must have two arguments.
template<typename Method>
void doStrategy(Method strategy, int param1, int param2)
{
// argument strategy MUST BE calculator's member function pointer,
// member function pointer need object, not class
(this->*strategy)(param1, param2);
}
在main.cpp
中Calculator cal;
cal.doStrategy(&calculator::add, 2, 3);
..更一般地......
在calculator.h中
// this template function do not use only for calculator's method
// but method must have two arguments.
template<typename Class, typename Method>
void doStrategy(Class* pObject, Method strategy, int param1, int param2)
{
// argument strategy MUST BE Class's member function pointer,
// member function pointer need object, not class
(pObject->*strategy)(param1, param2);
}
在main.cpp
中// definition of another member function...
struct foo
{
void bar(int param1, param2)
{
std::cout << "foo::bar " << param1 - param2 << std::endl;
}
};
int main()
{
Calculator cal;
foo test;
cal.doStrategy(&cal, &calculator::add, 2, 3);
cal.doStrategy(&test, &foo::bar, 2, 3);
foo* just_for_member_function = NULL;
cal.doStrategy(just_for_member_function, &foo::bar, 5, 1);
return 0;
}
更普遍?
嗯,下次......