What exactly is the ->* operator?

后端 未结 4 1753
执笔经年
执笔经年 2021-01-14 06:32

I\'ve never used it before and just stumbled upon it in an article... I thought it would be the equivalent to *x->y but apparently it isn\'t.

Here\'s

4条回答
  •  执念已碎
    2021-01-14 06:55

    The .* and ->* operators will point to member functions of a class or structure. The code below will show a simple example of how to use the .* operator, if you change the line: Value funcPtr = &Foo::One; to Value funcPtr = &Foo::Two; the result displayed will change to 1000 since that function is inValue*2

    for example Taken From Here:

    #include 
    #include 
    
    class Foo { 
      public: 
        double One( long inVal ) { return inVal*1; }
        double Two( long inVal ) { return inVal*2; }
    }; 
    
    typedef double (Foo::*Value)(long inVal); 
    
    int main( int argc, char **argv ) { 
      Value funcPtr = &Foo::One; 
    
      Foo foo;
    
      double result = (foo.*funcPtr)(500); 
      std::cout << result << std::endl;
      system("pause");
      return 0; 
    } 
    

提交回复
热议问题