gpt4 book ai didi

c++ - 将抽象类的方法作为 std::function 传递

转载 作者:太空狗 更新时间:2023-10-29 23:35:22 33 4
gpt4 key购买 nike

我有一些这样的代码(这只是一个片段,不是完全有效的代码):

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)
^

我的问题有什么解决方案吗?到目前为止我还没有找到。

最佳答案

您的代码按值绑定(bind)了 aClass 指向的对象。你想通过引用绑定(bind)它:

std::function<void()> func = std::bind(&AbstractClass::doA, std::ref(*aClass));

或者直接绑定(bind)aClass,指针也可以用于隐式this:

std::function<void()> func = std::bind(&AbstractClass::doA, aClass);

[Live example]

关于c++ - 将抽象类的方法作为 std::function 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39166015/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com