如何将非静态成员函数传递给ftw? 我是C++编程新手,我有这个问题: 我想做这样的事情 #include <iostream> #include <sys/types.h> #include <sys/stat.h> #include <ftw.h> class Foo { public: int func1 (const char *fpath, const struct stat *sb, int typeflag){ total += sb->st_size; std::cout << total << std::endl; return 0; } int func2(){ ftw("plots", func1, 3); } int total; }; #包括 #包括 #包括 #包括 福班{ 公众: int func1(常量字符*fpath,常量结构stat*sb,int类型标志){ 总+=sb->st_尺寸; std::cout
如何将非静态成员函数传递给ftw? 我是C++编程新手,我有这个问题: 我想做这样的事情 #include <iostream> #include <sys/types.h> #include <sys/stat.h> #include <ftw.h> class Foo { public: int func1 (const char *fpath, const struct stat *sb, int typeflag){ total += sb->st_size; std::cout << total << std::endl; return 0; } int func2(){ ftw("plots", func1, 3); } int total; }; #包括 #包括 #包括 #包括 福班{ 公众: int func1(常量字符*fpath,常量结构stat*sb,int类型标志){ 总+=sb->st_尺寸; std::cout,c++,non-static,C++,Non Static,func1具有名为this的隐藏参数,这意味着其签名与ftw参数的要求不匹配 我鼓励您创建静态/全局函数,并准备一些全局或单音变量来保存目标函数func1您可以将std::function存储在静态变量中,并具有一个“转发”调用的静态函数: class Foo { public: int func1 (const char *fpath, const struct stat *sb, int typeflag){ total += sb->st_size;
func1
具有名为this的隐藏参数,这意味着其签名与ftw参数的要求不匹配
我鼓励您创建静态/全局函数,并准备一些全局或单音变量来保存目标函数
func1
您可以将std::function
存储在静态变量中,并具有一个“转发”调用的静态函数:
class Foo {
public:
int func1 (const char *fpath, const struct stat *sb, int typeflag){
total += sb->st_size;
std::cout << total << std::endl;
return 0;
}
int func2(){
ftwForwardTo = [&this] (const char *fpath, const struct stat *sb, int typeflag) {
return func1(fpath, sb, typeflag);
};
ftw("plots", forwardFtw, 3);
}
int total;
private:
static std::function<int(const char *, const stat *, int)> ftwForwardTo;
static int forwardFtw(const char *fpath, const struct stat *sb, int typeflag){
ftwForwardFunc(fpath, sb, typeflag);
}
};
class-Foo{
公众:
int func1(常量字符*fpath,常量结构stat*sb,int类型标志){
总+=sb->st_尺寸;
std::cout什么是更广泛的上下文?我不知道关于C++17中的ftw
@Ben的任何内容,您可以使用
。如果您一直使用C,您无法传递成员函数或lambda。