呼び出し元の関数やクラスオブジェクトのポインタを呼び出すために使用されるC ++のポインタ()のオーバーロード

関数ポインタを使用する場合は、ポインタは括弧直接呼び出しで同じ関数名、およびパラメータリストのようにすることができます。また、最初のリコールを逆参照

//include directories...
  using namespace std;
  void testFun()
  {
      cout<<"this is a test"<<endl;
  }
  int main(int argc,char**argv)
  {
      auto *pFun=testFun;
     pFun();//or (*pFun)() is also fine   
 }

しかし、クラスのポインタを使用することはできません

//header files...
  using namespace std;
  class A
  {
  private:
      int a;
  public:
      A(int a_):a(a_){}
      void operator(){cout<<a<<endl;}
 };
 int main(int argc,char** argv)
 {
     A a1(5);
     A *pA=new A(7);
     a1();//correct using operator() function
     (*pA)();//pA() is not correct
 }

 

公開された28元の記事 ウォン称賛14 ビュー20000 +

おすすめ

転載: blog.csdn.net/liyunxin_c_language/article/details/83188217