gpt4 book ai didi

c++ - 为非静态成员函数创建一个 unary_function 仿函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:28 25 4
gpt4 key购买 nike

代码应该解释我的困难。虽然代码本身没什么意义,但我打算在 MyClass 中添加容器,并使用带有成员函数的算法。

#include <cstdlib>
#include <algorithm>
#include <functional>

using namespace std;

class MyClass
{
public:
MyClass() { a = 0; }
~MyClass() {}
private:
int a;
bool tiny_test (int);
int Func();
};

bool MyClass::tiny_test (int b)
{
return a == b;
}

int MyClass::Func()
{
// does not compile
(mem_fun(&MyClass::tiny_test))(this);

// commented below is another attempt, also no success
//mem_fun1_t<bool, MyClass, int> tmp_functor = mem_fun(&MyClass::tiny_test);
//tmp_functor(this);
return 0;
}

int main(int argc, char** argv)
{
return 0;
}

非常感谢!顺便说一句,我没有使用静态成员函数,只是因为我相信它必须适用于非静态成员函数。附言Eric、Jarod42,感谢您的及时回复!

最佳答案

bool MyClass::tiny_test (int b)
{ // ^^^^^ You missed this argument
return a == b;
}

试试这个:

// Supply one more argument. E.g., 3
(mem_fun(&MyClass::tiny_test))(this, 3);

关于c++ - 为非静态成员函数创建一个 unary_function 仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31712955/

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