C++ 螺纹和接口C++;
我在使用接口和工厂创建不同线程时遇到了一些问题: 我有两个派生的接口(这里是由一个类派生的,但最终会有更多…)。我使用工厂创建所需派生类的对象。 当我在不同的线程中运行它们时,我使用工厂返回给我的内容作为线程构造函数的参数C++ 螺纹和接口C++;,c++,multithreading,interface,functor,C++,Multithreading,Interface,Functor,我在使用接口和工厂创建不同线程时遇到了一些问题: 我有两个派生的接口(这里是由一个类派生的,但最终会有更多…)。我使用工厂创建所需派生类的对象。 当我在不同的线程中运行它们时,我使用工厂返回给我的内容作为线程构造函数的参数 #include <iostream> #include <thread> class Base { public: virtual ~Base () {} virtual void operator ()
#include <iostream>
#include <thread>
class Base
{
public:
virtual ~Base () {}
virtual void operator () () = 0;
};
class Derived : public Base
{
public:
virtual void operator () ()
{
std::cout << "Derived of Base!" << std::endl;
}
};
enum BaseType
{
derived = 1000
};
class BaseFactory
{
public:
static Base *createBase (BaseType bt)
{
switch (bt)
{
case derived:
return new Derived;
default:
return NULL;
}
}
};
class OtherBase
{
public:
virtual ~OtherBase () {}
virtual void operator () () = 0;
};
class OtherDerived : public OtherBase
{
public:
virtual void operator () ()
{
std::cout << "OtherDerived of OtherBase!" << std::endl;
}
};
enum OtherBaseType
{
otherderived = 1100
};
class OtherBaseFactory
{
public:
static OtherBase *createOtherBase (OtherBaseType obt)
{
switch (obt)
{
case otherderived:
return new OtherDerived;
default:
return NULL;
}
}
};
int main (int argc, const char *argv[])
{
Base *pBase = BaseFactory::createBase(derived);
OtherBase *pOBase = OtherBaseFactory::createOtherBase(otherderived);
std::thread *t1 = new std::thread ((*pBase)());
std::thread *t2 = new std::thread ((*pOBase)());
t1->join();
t2->join();
delete t1;
delete t2;
return 0;
}
我相信这个问题来自于我将其作为参数来创建类型为Base和OtherBase的threads对象(因此是接口)。
然而,我真的不知道如何解决这个问题。正如编译器所说,您要求创建参数设置为void的线程。 这是因为您通过
(*pBase)(
)调用对象函数(operator())
我想您需要创建一个绑定到适当对象的函数。您可以使用std::bind这样做:
std::bind(&Base::operator(), pBase)
因此,线程创建应该如下所示:
std::thread *t1 = new std::thread (std::bind(&Base::operator(), pBase));
正如编译器所说,您要求创建参数设置为void的线程。 这是因为您通过
(*pBase)(
)调用对象函数(operator())
我想您需要创建一个绑定到适当对象的函数。您可以使用std::bind这样做:
std::bind(&Base::operator(), pBase)
因此,线程创建应该如下所示:
std::thread *t1 = new std::thread (std::bind(&Base::operator(), pBase));
正如编译器所说,您要求创建参数设置为void的线程。 这是因为您通过
(*pBase)(
)调用对象函数(operator())
我想您需要创建一个绑定到适当对象的函数。您可以使用std::bind这样做:
std::bind(&Base::operator(), pBase)
因此,线程创建应该如下所示:
std::thread *t1 = new std::thread (std::bind(&Base::operator(), pBase));
正如编译器所说,您要求创建参数设置为void的线程。 这是因为您通过
(*pBase)(
)调用对象函数(operator())
我想您需要创建一个绑定到适当对象的函数。您可以使用std::bind这样做:
std::bind(&Base::operator(), pBase)
因此,线程创建应该如下所示:
std::thread *t1 = new std::thread (std::bind(&Base::operator(), pBase));
std::thread
的构造函数可以将成员函数指针作为第一个参数,并将自动取消引用并调用第二个参数上的成员函数指针
因此,你可以写:
std::thread *t1 = new std::thread (&Base::operator(), pBase);
std::thread *t2 = new std::thread (&OtherBase::operator(), pOBase);
这可能比使用
std::bind
std::thread
的构造函数可以将成员函数指针作为第一个参数,并将自动取消引用并调用第二个参数上的成员函数指针更简单
因此,你可以写:
std::thread *t1 = new std::thread (&Base::operator(), pBase);
std::thread *t2 = new std::thread (&OtherBase::operator(), pOBase);
这可能比使用
std::bind
std::thread
的构造函数可以将成员函数指针作为第一个参数,并将自动取消引用并调用第二个参数上的成员函数指针更简单
因此,你可以写:
std::thread *t1 = new std::thread (&Base::operator(), pBase);
std::thread *t2 = new std::thread (&OtherBase::operator(), pOBase);
这可能比使用
std::bind
std::thread
的构造函数可以将成员函数指针作为第一个参数,并将自动取消引用并调用第二个参数上的成员函数指针更简单
因此,你可以写:
std::thread *t1 = new std::thread (&Base::operator(), pBase);
std::thread *t2 = new std::thread (&OtherBase::operator(), pOBase);
这可能比使用
std::bind
更简单。您正在调用functor,而不是将它们传递给要调用的线程。您正在调用functor,而不是将它们传递给要调用的线程。您正在调用functor,而不是将它们传递给要调用的线程。您正在调用functor,而不是将它们传递给要调用的线程要调用的线程。谢谢,现在可以使用了!你救了我一天:)谢谢你,现在一切正常!你救了我一天:)谢谢你,现在一切正常!你救了我一天:)谢谢你,现在一切正常!你救了我一天:)对了,谢谢。我不知道这个构造器。是的,的确更简单更可读!谢谢你帮助我:)顺便说一下,谢谢。我不知道这个构造器。是的,的确更简单更可读!谢谢你帮助我:)顺便说一下,谢谢。我不知道这个构造器。是的,的确更简单更可读!谢谢你帮助我:)顺便说一下,谢谢。我不知道这个构造器。是的,的确更简单更可读!谢谢你帮我:)