RequestS想要使用ReportUrlThread的函数(send).ReportUrlThread是一个模板类。
下面的代码看起来就是“request-> process(reportUrl-> send);”, 我怎么能实现它?
以下代码无法通过编译。
int main()
{
typedef Threadpool<RequestS> ThreadpoolDealFromBS2;
ThreadpoolDealFromBS2 threadpool;
ReportUrlReq* req = new ReportUrlReq();
threadpool.appendReportHiUrl(req);
}
class RequestS {
public:
RequestS()
{
}
virtual ~RequestS()
{
}
virtual void process(void (*send)(bool &exp))
{
log(Info, "RequestS...fun");
}
};
class ReportUrlReq:public RequestS {
public:
ReportUrlReq();
~ReportUrlReq();
virtual void process(void (*send)(bool &exp))
{
log(Info, "ReportUrlReq...fun");
}
};
template< typename T >
class ReportUrlThread {
public:
ReportUrlThread(uint32_t id)
{
}
virtual ~ReportUrlThread()
{
}
void send(bool &exp)
{
}
Threadpool< T >* threadpool;
};
template< typename T >
class Threadpool
{
public:
Threadpool( std::vector<ReportUrlThread<T>*>& reportUrl);
~Threadpool();
bool appendReportHiUrl( T* request );
private:
static void* reportWorker( void* arg );
void reportRun(ReportUrlThread<T> *reportUrl);
pthread_t* m_ReportUrlThreads;
};
template< typename T >
Threadpool< T >::Threadpool( std::vector<ReportUrlThread<T>*>& reportUrl)
{
m_ReportUrlThreads = new pthread_t[reportUrlThreadNum];
for (int i = 0; i < 10; ++i)
{
ReportUrlThread<T> * reportUrlThread = reportUrl[i];
reportUrlThread->threadpool = this;
if( pthread_create( m_ReportUrlThreads + i, NULL, reportWorker, reportUrlThread ) != 0 )
{
delete [] m_ReportUrlThreads;
throw std::exception();
}
if( pthread_detach( m_ReportUrlThreads[i] ) )
{
delete [] m_ReportUrlThreads;
throw std::exception();
}
}
}
template< typename T >
void* Threadpool< T >::reportWorker( void* arg )
{
ReportUrlThread<T>* reportUrl = (ReportUrlThread<T>*)arg;
Threadpool* pool = reportUrl->threadpool;
pool->reportRun(reportUrl);
return pool;
}
template< typename T >
void Threadpool< T >::reportRun(ReportUrlThread<T> *reportUrl)
{
while ( ! m_ReportStop )
{
m_ReportQueuestat.wait();
m_ReportQueuelocker.lock();
if ( m_ReportWorkqueue.empty() )
{
m_ReportQueuelocker.unlock();
continue;
}
T* request = m_ReportWorkqueue.front();
m_ReportWorkqueue.pop_front();
reportDealNum++;
m_ReportWorkqueueSize = m_ReportWorkqueue.size();
request->process(reportUrl->send);
}
}
答案 0 :(得分:1)
实际错误是,您将成员函数reportUrl->send
传递给期望普通函数的request->process
。但是一个成员函数需要一个类的实例!如果send
取决于ReportUrlThread
的成员,您可能希望传递ReportUrlThread
的实例(或实现send
的派生类):
virtual void process(ReportUrlThread<RequestS> *RepUrlThReqS) {
bool exp;
RepUrlThReqS->send(exp);
// ...
}
如果没有,您可能想要使用静态函数:
static void send(bool &exp) {
// ...
}
你甚至可能想要使用lambda函数(这里有点hacky):
class RequestS {
public:
virtual void process(void(*send)(bool &exp, void* instance), void *instance) {
bool exp;
send(exp, instance);
}
};
template< typename T >
class ReportUrlThread {
public:
void send(bool &exp) { }
};
int main() {
ReportUrlThread<RequestS> *reportUrl = new ReportUrlThread<RequestS>;
RequestS *request = new RequestS;
request->process(
[](bool &exp, void* reportUrlA) {
((ReportUrlThread<RequestS> *)reportUrlA)->send(exp);
}, reportUrl);
}
还有更多可能性......
由您来决定,这是您的最佳解决方案。