简体   繁体   English

C ++中的函数指针

[英]Function pointers in C++

I used CreateThread function to write a class like C# BackgroundWorker in C++.My code: 我使用CreateThread函数在C ++中编写类似C#BackgroundWorker的类。我的代码:

BackgroundWorker.h: BackgroundWorker.h:

class BackgroundWorker
{
    private :
        HANDLE _threadHandle;
        unsigned int _threadCallcounter;
        DWORD _threadID;
    public:
        BackgroundWorker();
        ~BackgroundWorker();
        virtual DWORD WINAPI Function(LPVOID vpPram);
}

BackgroundWorker.cpp: BackgroundWorker.cpp:

#include "BackgroundWorker.h"
void BackgroundWorker::DoWork()
{
    this->_threadHandle = CreateThread(NULL,
        0,this->Function,&this->_threadCallcounter,
        0, &this->_threadID); // !!!***This part throws an error***!!!
}

Then I created another class that derived from BackgroundWorker: 然后我创建了另一个派生自BackgroundWorker的类:

ListenThread.cpp: ListenThread.cpp:

class ListenThread :public BackgroundWorker
{
    DWORD WINAPI Function(LPVOID vpPram)
    {
        //DO somthing...
        return 0;
    }
};

But that line gives me the following error: 但该行给了我以下错误:

non - standard syntax; 非标准语法; use '&' to create a pointer to member 使用'&'创建指向成员的指针

The function pointer that CreateThread expects must have this signature: CreateThread期望的函数指针必须具有以下签名:

DWORD WINAPI ThreadProc(LPVOID lpParameter);

When you create a member function it gets an invisible "this" parameter as first argument, so you declare something like this implicitly: 当你创建一个成员函数时,它会获得一个不可见的“this”参数作为第一个参数,所以你隐式地声明这样的东西:

DWORD WINAPI ThreadProc(BackgroundWorker *this, LPVOID lpParameter);

Create a static member function to omit the this pointer and if you need that pointer inside the thread routine pass it as the void * parameter 创建一个静态成员函数来省略this指针,如果需要在线程例程中使用该指针,则将其作为void *参数传递

Your error message means you need to pass pointer to function as &Function , not Function in DoWork . 您的错误消息意味着您需要将指针传递给函数作为&Function ,而不是FunctionDoWork

Unfortunately fixing this won't help. 不幸的是修复这个没有用。 CreateThread doesn't work with (non-static) member functions. CreateThread不适用于(非静态)成员函数。 A solution is to create a static method to use as the actual thread start function. 解决方案是创建一个静态方法以用作实际的线程启动函数。

Check this example: 检查此示例:

#include <Windows.h>
#include <iostream>

class BackgroundWorker
{
private :
    HANDLE _threadHandle;
    DWORD _threadID;

public:
    static DWORD WINAPI StaticThreadStart(void * Param) {
        BackgroundWorker * This = static_cast<BackgroundWorker *>(Param);
        return This->Function();
    }

    virtual DWORD Function() {
        return 0;
    }

    void DoWork() {
        _threadHandle = CreateThread(NULL, 0, StaticThreadStart, this, 0, &_threadID);
    }
};

class ListenThread : public BackgroundWorker {
    DWORD Function() override {
        std::cout << "Working...\n";
        return 0;
    }
};

int main()
{
    ListenThread cl;
    cl.DoWork();

    // Call pause to wait for new thread in this example
    system("pause");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM