gpt4 book ai didi

c++ - 在 C++ 中将函数作为参数传递

转载 作者:行者123 更新时间:2023-11-28 00:42:03 25 4
gpt4 key购买 nike

我有以下电话:

void Derived::GetEntry(Skill&);
InorderTraverse(GetEntry);

哪个调用

void Base<ItemType>::InorderTraverse(void Visit(ItemType&)) const

尝试编译生成

error C3867: 'Derived::GetEntry': function call missing argument list; use '&Derived::GetEntry' to create a pointer to member

使用 &Derived::GetEntry 生成

cannot convert parameter 1 from 'void (__thiscall Derived::* )(Skill &)' to 'void (__cdecl *)(ItemType &)'

将声明更改为 static void GetEntry... 解决了这些问题,但产生了一组新问题(即我无法访问非静态对象(非静态成员引用必须与特定对象相关)

我有一个类似的遍历操作,它适用于静态声明,因为被调用的函数只显示有关调用它的每个对象的信息。

我这几天一直在寻找答案,我觉得这很简单。有没有办法在另一个函数调用中使用非静态函数作为参数?

完整代码为:https://github.com/mindaika/SkillTree

最佳答案

编辑:改为插入完整的工作示例。

我建议您改为使用 std 函数指针。

例如:

#include <functional>
void main()
{
class TheClass
{
public:

TheClass()
{
m_function = ( std::tr1::bind(&TheClass::run, this) );
};

void run()
{
// stuff to do
};

std::tr1::function<void ()> m_function;
};

TheClass theclass;
theclass.m_function();

}

m_函数();//调用函数

关于c++ - 在 C++ 中将函数作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18384704/

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