我有一些像这样的代码(这只是一个片段,不是完全有效的代码):
class AbstractClass {
public:
AbstractClass() {}
virtual ~AbstractClass() {}
virtual void doA() { std::cout << "doA1\n"; };
virtual void doB() = 0;
};
class ImplClass : public AbstractClass {
public:
ImplClass() {}
virtual ~ImplClass() {}
virtual void doA() override { std::cout << "doA2\n"; };
virtual void doB() override { std::cout << "doB2\n"; };
};
现在我想在std :: function中存储对AbstractClass的成员方法的调用,如下所示:
int main() {
AbstractClass* aClass = new ImplClass();
std::function<void()> func = std::bind(&AbstractClass::doA, *aClass)
delete aClass;
return 0;
}
然而,根据编译器,由于AbstractClass中存在纯虚方法,这不起作用。
修改:供参考完整错误
In file included from source_file.cpp:2:0:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_helper<false, void (AbstractClass::*)(), AbstractClass&>’:
/usr/include/c++/5/functional:1462:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (AbstractClass::*)(); _BoundArgs = {AbstractClass&}]’
source_file.cpp:26:72: required from here
/usr/include/c++/5/functional:1445:71: error: invalid abstract parameter type ‘AbstractClass’
typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
^
source_file.cpp:5:7: note: because the following virtual functions are pure within ‘AbstractClass’:
class AbstractClass {
^
source_file.cpp:10:18: note: virtual void AbstractClass::doB()
virtual void doB() = 0;
^
source_file.cpp: In function ‘int main()’:
source_file.cpp:26:72: error: no matching function for call to ‘bind(void (AbstractClass::*)(), AbstractClass&)’
std::function<void()> func = std::bind(&AbstractClass::doA, *aClass)
^
In file included from source_file.cpp:2:0:
/usr/include/c++/5/functional:1462:5: note: candidate: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/5/functional:1462:5: note: substitution of deduced template arguments resulted in errors seen above
/usr/include/c++/5/functional:1490:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/5/functional:1490:5: note: template argument deduction/substitution failed:
source_file.cpp:26:72: note: couldn't deduce template parameter ‘_Result’
std::function<void()> func = std::bind(&AbstractClass::doA, *aClass)
^
我的问题有解决办法吗?到目前为止我还没找到。
答案 0 :(得分:9)
您的代码通过值绑定aClass
指向的对象。您想通过引用绑定它:
std::function<void()> func = std::bind(&AbstractClass::doA, std::ref(*aClass));
或者直接绑定aClass
,指针也可以用于隐式this
:
std::function<void()> func = std::bind(&AbstractClass::doA, aClass);
答案 1 :(得分:2)
目前最好的建议是赞成lambdas超过std::bind
,自从lambda到达以来,这被认为是标准中的一种时代错误。
lambda表达式:
std::ref
和std::cref
示例:
#include <functional>
#include <iostream>
class AbstractClass {
public:
AbstractClass() {}
virtual ~AbstractClass() {}
virtual void doA() { std::cout << "doA1\n"; };
virtual void doB() = 0;
};
class ImplClass : public AbstractClass {
public:
ImplClass() {}
virtual ~ImplClass() {}
virtual void doA() override { std::cout << "doA2\n"; };
virtual void doB() override { std::cout << "doB2\n"; };
};
int main() {
AbstractClass* aClass = new ImplClass();
// aClass is a pointer, and a copy of that pointer will be
// captured.
std::function<void()> func = [aClass]{ aClass->doA(); };
func();
delete aClass;
return 0;
}