C/C++ 類成員函數指標 類成員資料指標

來源:互聯網
上載者:User

標籤:物件類型   方式   blog   c++ 類   cti   include   stat   一個   xxxx   

普通函數指標:  "return_type (*ptr_name)(para_types) "

類成員函數指標: "return_type (class_name::*ptr_name)(para_types)"

類資料成員指標: "type class_name::* ptr_name";

 

C/C++:

 1 class Demo 2 { 3 public: 4     Demo():data(100) 5     { 6  7     } 8     int data; 9     int show(int a,int b)10     {11         return a+b;12     }13 };14 15 16 17 int main(int argc, char **argv)18 {19     Demo A;20     Demo* B=new Demo;21     //完成聲明和賦值22     int Demo::* int_ptr=&Demo::data;23     //賦值完後沒有資料資訊,需要對象解引用獲得資料24     std::cout<<A.*int_ptr<<std::endl;25     std::cout<<B->*int_ptr<<std::endl;26 27     28     //類成員函數指標29     int (Demo::*ptr)(int,int)=&Demo::show;30     std::cout<<(A.*ptr)(1,2)<<std::endl;31 32     return 0;33 }


 

 

 

STL演算法有時需要使用類成員的函數,然而類成員函數指標不是可調用對象,functor<> bind mem_fn 各自方式不一,但是內部都是隱式傳遞this指標通過解引用來擷取資料或調用函數

 

C/C++擴充:

  1 //成員函數指標使用  2   3 class Screen  4 {  5 public:  6     enum ACTION  7     {  8         Up_, Down_, Left_, Right_  9     }; 10  11     Screen() = default; 12  13     Screen &Move(ACTION para) 14     { 15         return (this->*Menu[para])(); 16     } 17  18 private: 19     using Action= 20     Screen &(Screen::*)(); 21  22  23     static Action Menu[]; 24  25     Screen &Up() 26     { 27         std::cout << "Up" << std::endl; 28         return *this; 29     } 30  31     Screen &Down() 32     { 33         std::cout << "Down" << std::endl; 34         return *this; 35     } 36  37     Screen &Left() 38     { 39         std::cout << "Left" << std::endl; 40         return *this; 41     } 42  43     Screen &Right() 44     { 45         std::cout << "Right" << std::endl; 46         return *this; 47     } 48  49 }; 50  51 Screen::Action Screen::Menu[]{&Screen::Up, &Screen::Down, &Screen::Left, &Screen::Right}; 52  53  54 int main() 55 { 56     Screen obj; 57     obj.Move(Screen::Up_); 58     obj.Move(Screen::Down_); 59     obj.Move(Screen::Left_); 60     obj.Move(Screen::Right_); 61     obj.Move(Screen::Right_); 62  63  64     return 0; 65 } 66  67  68  69 #include <iostream> 70 #include <vector> 71 #include <functional> 72 #include <algorithm> 73 #include <string> 74  75 //類成員函數指標不是可調用對象,一般STL演算法需要封裝類成員函數指標為可調用對象 76 //這三種封裝方式都是內部通過獲得容器返回的對象, ".*" / "->*" 來調用; 77 //1.std::function<成員函數類型(第一個參數設定為對象本身類型)> fcn=&std::xxxx::xxx;   區別: 第一個參數是傳入物件類型,根據容器是引用還是指標選擇(.* / ->*) 78 //2.std::bind(&std::string::empty,std::placeholders::_1) 區別:可通過指標或者對象執行成員函數; 79 //3.std::mem_fn(&std::string::empty)  C++11, 區別:可通過指標和對象執行成員函數 80 //4.可使用lamda調用; 81  82  83 class Str 84 { 85 public: 86     std::string str; 87  88     Str(const std::string &str_) : str(str_) 89     { 90  91     } 92  93     Str(const char *str_) : str(str_) 94     { 95  96     } 97  98     bool empty() const noexcept 99     {100         return str.empty();101     }102 };103 104 105 int main()106 {107 108     std::vector<Str> str_vec{"xa", "sad", "", "", "", "qqewhegr", "aqdq"};109 110 111     std::function<bool(const Str &)> fn = &Str::empty;112 113     std::size_t empty_size_function=std::count_if(str_vec.begin(),str_vec.end(),fn);114 115     std::size_t empty_size_bind=std::count_if(str_vec.begin(),str_vec.end(),std::bind(&Str::empty,std::placeholders::_1));116 117     std::size_t empty_size_mem_fn=std::count_if(str_vec.begin(),str_vec.end(),std::mem_fn(&Str::empty));118 119 120     std::cout << empty_size_function << std::endl;121     std::cout << empty_size_bind << std::endl;122     std::cout << empty_size_mem_fn << std::endl;123 124 125 126     std::size_t size = std::count_if(str_vec.begin(), str_vec.end(), [](const Str& str) { return str.empty(); });127     std::cout << size << std::endl;128 129 130     return 0;131 }

 

C/C++ 類成員函數指標 類成員資料指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.