我正在尝试编写一个类模板,其在构造函数参数中获取两个 std::function 并在构造和析构过程中以 RAII 的方式调用它们。
我缺少什么?
我缺少什么?
template <class T>
class HelloGoodbye{
public:
HelloGoodbye(std::function<T> const & f, std::function<T> const &g)
:mf(f),mg(g)
{
mf();
}
~HelloGoodBye()
{
mg();
}
private:
std::function<T> mf;
std::function<T> mg;
};
class Printer
{
public:
void hello()
{
std::cout << "hello!" << std::endl;
}
void goodbye()
{
std::cout << "Goodbye!" << std::endl;
}
};
int main()
{
Printer p;
auto hi = std::bind(&Printer::hello, &p);
auto bye = std::bind(&Printer::goodbye, &p);
HelloGoodbye hgb(hi,bye);
}
mf
。 - John