Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何通过接口将函子指定给函数对象?_C++_Function_Interface_Polymorphism - Fatal编程技术网

C++ 如何通过接口将函子指定给函数对象?

C++ 如何通过接口将函子指定给函数对象?,c++,function,interface,polymorphism,C++,Function,Interface,Polymorphism,我有一个接口和一个实现它的类。考虑下面的代码: #include <functional> using namespace std; class Interface { public: virtual bool operator()() = 0; }; class Derived : public Interface { public: bool operator()() override { //some code here return true;

我有一个接口和一个实现它的类。考虑下面的代码:

#include <functional>

using namespace std;

class Interface {
public:
  virtual bool operator()() = 0;
};

class Derived : public Interface {
public:
  bool operator()() override {
    //some code here
    return true;
  };
};

int main() {

  Derived d;

  function<bool()> bar = d; //compiles without any errors

  Interface* i = new Derived();

  function<bool()> foo = *i; //does not work

  return 0;
}
#包括
使用名称空间std;
类接口{
公众:
虚拟布尔运算符()()=0;
};
派生类:公共接口{
公众:
布尔运算符()()覆盖{
//这里有一些代码
返回true;
};
};
int main(){
导出d;
function bar=d;//编译时没有任何错误
接口*i=新派生的();
函数foo=*i;//不起作用
返回0;
}
编译器返回错误C2440:“正在初始化”:无法从“接口”转换为“std::函数”。如何通过接口将函子指定给函数对象

编辑:谢谢您的建议。使用std::bind有帮助,但当我想在operator()函数中传递参数时,仍然有一个问题。请参阅以下代码:

#include <functional>
#include <iostream>

using namespace std;

class Interface {
public:
  virtual bool operator()(unsigned int) = 0;
};

class Derived : public Interface {
public:
  bool operator()(unsigned int count) override {
    for (unsigned int i = 0; i < count; i++) {
      cout << "Hello World \n";
    }
    return true;
  };
};

bool doSomething(function<bool(unsigned int)> const& predicate) {

  if (predicate(5)) {
    return true;
  }
};

int main() {

  Derived d;

  doSomething(d); //works

  Interface* i = new Derived();

  doSomething(std::bind(&Interface::operator(), i)); //does not compile

  return 0;
}
#包括
#包括
使用名称空间std;
类接口{
公众:
虚拟布尔运算符()(无符号整数)=0;
};
派生类:公共接口{
公众:
布尔运算符()(无符号整数计数)重写{
for(无符号整数i=0;ifunction foo=*i;
正试图将您的
派生的
实例切片为
实例(由于
std::function
上的赋值运算符),由于它是一个实例,因此无法执行。您可以使用lambda绑定您的函数:

function<bool()> bar = [&]() { d(); };
功能条=[&](){d();};
阅读您的评论后:

我有一个不同的类,它需要一个函数对象,我想通过一个类的函子来提供它

你可以这样做:

void Foo::Bar(Instance &instance)
{
    // Be careful of lifetime issues, depending on lifetime of stored function
    std::function<bool ()> func = [&]() { instance(); };
    // Do something with func
}
void Foo::Bar(实例和实例)
{
//根据存储函数的生存期,请注意生存期问题
std::function func=[&](){instance();};
//用func做点什么
}

问题编辑后的

class Interface {
public:
    virtual bool operator()(unsigned int) = 0;
};
operator()
现在有一个类型为
unsigned int
的参数,但是
std::bind
返回的函数对象为零

可能的解决办法:

doSomething(std::bind(&Interface::operator(), i, std::placeholders::_1));
// or
doSomething([=](auto... args) { return (*i)(args...); });
// or
doSomething([=](auto... args) { return std::invoke(*i, args...); });

出于好奇:
std::function
在您已经有了
接口的情况下,为什么要使用
std::function
呢?
std::function
做的比您需要的多得多
std::function
试图在复制对象时对其进行切片,但由于
Interface
是抽象的,并且不存在,因此被阻止了独立运行。
函数foo=[=]{return(*i)(;};
函数foo=std::bind(&Interface::operator(),i);
@formerlyknownas_463035818我有一个不同的类,它需要一个函数对象,我想通过一个类的函子来提供它。imho编辑应该是一个单独的问题。最初问的问题已经有了答案