Maison  >  Questions et réponses  >  le corps du texte

c++11 - C++中如何定义一个指向函数的智能指针?

如题。定义一个内置类型比如int型的智能指针可以 shared_ptr<int>这样定义,但不知道如何定义一个指向函数的智能指针。

大家讲道理大家讲道理2714 Il y a quelques jours879

répondre à tous(1)je répondrai

  • 阿神

    阿神2017-04-17 13:09:49

    #include <stdio.h>
    #include <memory>
    
    typedef void (*Fn)(void);
    
    void Foo() {
      printf("hello world");
    }
    
    int main() {
      std::shared_ptr<Fn> ptr(new Fn(Foo));
      (*ptr)();
      return 0;
    }

    PS: C++11里面有function, 所以一般不用函数指针

    répondre
    0
  • Annulerrépondre