简体   繁体   English

将成员函数的指针转换为C函数的指针是一种好习惯吗

[英]Is it good practice to cast a pointer to a member function as a pointer to a C function

This code compiles (with g++ (GCC) 6.3.1) and appears to "work". 这段代码会编译(使用g ++(GCC)6.3.1),似乎可以正常工作。 But I'm VERY skeptical as to whether it constitutes good practice . 但是我非常怀疑它是否构成良好的做法

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


class ThreadObj
{
  public:
    void *memberFunction(void*);
    void startThread(void);
  static int w;
};

int ThreadObj::w = 13;

void* ThreadObj::memberFunction(void*)
{
  w = 17;
  return 0;
}

void  ThreadObj::startThread(void)
{
  void * (*p)(void *) = *((void * (*)(void *))&ThreadObj::memberFunction);
  pthread_t    tid;

  if (pthread_create(&tid, 0, p, this) == 0)
    pthread_detach(tid);
}

int main(int argc, char *argv[] )
{
  ThreadObj thr;
  thr.startThread();

  return 0;
}

Casting a pointer to a member function as a pointer to a C function and passing this as data seems to rely (TOO MUCH) upon the mercies of how the compiler chooses to interpret/compile this. 将指向成员函数的指针转换为C函数的指针,并将this作为数据传递, this似乎取决于(TOO MUCH)编译器选择如何解释/编译该对象。

This has been covered before but not definitively answered. 之前已经对此进行了介绍,但没有给出明确的答案。

How can this example be broken? 如何打破这个例子?

The old way (with recent syntax) to go were: 旧方法(使用最新语法)是:

class ThreadObj
{
public:
    void memberFunction() { w = 17;}
    void startThread() {
        pthread_t    tid;

        if (pthread_create(&tid, 0, &Func, this) == 0) {
            pthread_detach(tid);
        }
    }
    static void* Func(void* p) {
        auto* that = static_cast<ThreadObj*>(p);
        that->memberFunction();
        return nullptr;
    }
private:
    int w = 13;
};

But in c++11, std::thread has a better interface. 但是在c ++ 11中, std::thread具有更好的接口。

Not really. 并不是的。 It's undefined behavior. 这是未定义的行为。 It is rather cute though. 虽然很可爱。 Whether it works or not is kind of up in the air. 不管它是否奏效,都有待解决。 My guess is that it generally would. 我的猜测是通常会这样。

The author of the code is taking advantage of the fact that this is generally passed in as the first parameter to the implementation of member functions. 代码的作者正在采取的事实,利用this一般传递进来的第一个参数的成员函数的实现。

Using pointer to methods of a classe is very usefull and, for me, is a good practice because it allows you implements another kind of patterns, for example: trigger a event over severas class that has the same prototype of method. 使用指向类的方法的指针非常有用,对我而言,这是一个好习惯,因为它允许您实现另一种模式,例如:在具有相同方法原型的severas类上触发事件。

Answering your question: It is a matter of designe and how it will be usefull for your goal. 回答您的问题:这是设计问题,它将如何对您的目标有用。

I share you a code where i create tow thread over diferents methods of the same class, i hope you it will help you. 我与您共享一个代码,在该代码中我可以在同一类的不同方法上创建拖曳线程,希望您能对您有所帮助。

https://github.com/jorgemedra/C-11-Threads/blob/master/README.md https://github.com/jorgemedra/C-11-Threads/blob/master/README.md

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

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