简体   繁体   English

如何声明一个函数,该函数是类的成员,并返回指向线程的函数指针?

[英]how to declare a function which is member of a class and returns a function pointer to a thread?

I want to write a function (say foo ) which takes string as an argument and returns a function pointer, however this pointer points to the following function: 我想编写一个将字符串作为参数并返回一个函数指针的函数(例如foo ),但是该指针指向以下函数:

DWORD WINAPI fThread1(LPVOID lparam)

Also the function ( foo ) is member of a class, so I will be defining it and declaring it in separate files ( .hpp and .cpp files). 另外,函数( foo )是类的成员,因此我将对其进行定义并在单独的文件( .hpp.cpp文件)中进行声明。

Please help me with the declaration syntax. 请帮助我声明语法。

The easiest way is to use a typedef for the function pointer: 最简单的方法是对函数指针使用typedef:

typedef DWORD (WINAPI *ThreadProc)(LPVOID);

class MyClass
{
public:
    ThreadProc foo(const std::string & x);
};
...
ThreadProc MyClass::foo(const std::string & x)
{
    // return a pointer to an appropriate function
}

Alternatively, if you don't want to use a typedef for some reason, you can do this: 或者,如果由于某种原因不想使用typedef,则可以执行以下操作:

class MyClass
{
public:
    DWORD (WINAPI *foo(const std::string & x))(LPVOID);
};
...
DWORD (WINAPI *MyClass::foo(const std::string & x))(LPVOID)
{
    // return a pointer to an appropriate function
}

The syntax is rather ugly, so I highly recommend using a typedef. 语法很丑陋,因此我强烈建议使用typedef。

I think this is what you want: 我认为这是您想要的:

class Bob
{
public:
   typedef DWORD (__stdcall *ThreadEntryPoint)(LPVOID lparam);

   ThreadEntryPoint GetEntryPoint(const std::string& str)
   {
      // ...
   }
};

I picked up the definition of ThreadEntryPoint from winbase.h, there called PTHREAD_START_ROUTINE . 我从winbase.h中获得了ThreadEntryPoint的定义,该定义称为PTHREAD_START_ROUTINE

ThreadEntryPoint is a function pointer to a function with the signature you showed, and GetEntryPoint returns a pointer to such a function. ThreadEntryPoint是指向具有您显示的签名的函数的函数指针,而GetEntryPoint返回指向该函数的指针。

Check the comments for understnding: 检查注释了解以下内容:

//Put this in a header file
class Foo
{   
    public:
        //A understandable name for the function pointer
        typedef DWORD (*ThreadFunction)(LPVOID);

        //Return the function pointer for the given name
        ThreadFunction getFunction(const std::string& name);
};



//Put this in a cpp file

//Define two functions with same signature
DWORD fun1(LPVOID v)
{
    return 0;
}

DWORD fun2(LPVOID v)
{
    return 0;
}

Foo::ThreadFunction Foo::getFunction(const std::string& name)
{
    if(name == "1")
    {
        //Return the address of the required function
        return &fun1;
    }
    else
    {
        return &fun2;
    }
}

int main()
{
    //Get the required function pointer
    Foo f;
    Foo::ThreadFunction fptr = f.getFunction("1");

    //Invoke the function
    (*fptr)(NULL);


}

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

相关问题 ro如何声明一个指向函数指针的成员 - How ro declare a member which is a pointer to function 如何声明指向返回函数指针的函数的指针 - How to declare pointer pointing to function which returns function pointer 如何使成员函数返回指向成员函数的指针? - How to make a member function which returns a pointer to a member function? 声明并使用指向另一个类中成员函数的指针 - Declare and use a pointer to member function in another class 成员函数指针返回相同类型的成员函数指针 - member function pointer which returns same type of member function pointer 如何通过本身是 class 成员的指针调用成员 function - How to call a member function via a pointer which is a class member itself 如何将成员声明为指向extern“C”函数的指针? - How to declare a member as a pointer to an extern “C” function? 如何声明指向成员模板 function 的指针? - How to declare a pointer to member template function? 在C ++中将成员函数的线程声明为类的成员 - declare a thread of member function as a member of the class in C++ 如何将函数指针(我们不知道参数的函数)声明为类成员? - How to declare a function pointer (function that we don't know the arguments) as a class member?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM