指向類成員的指標並非指標

來源:互聯網
上載者:User

from《C++ Common Knowledge》

#include <iostream>using namespace std;extern void fi(int);extern void fl(long);extern void fc(char);class Foo{public:    Foo(){};    virtual ~Foo(){};static void do_foo(int i){cout<<"您好,您輸入了i="<<i<<endl;}void to_str(){cout<<"a_:"<<a_<<",b_:"<<b_<<endl;}int get_ab(){return a_ + b_ ;}int b_;int a_;};int main(){    typedef void (*PF)(int);PF pf1 = Foo::do_foo;pf1(123);cout<<hex<<pf1<<dec<<endl;int Foo:: * pimC;   //指向類Foo的int成員    typedef int (Foo:: * PFAB)(void);PFAB pfab = &Foo::get_ab;    //指向類Foo的函數成員 get_abFoo foo;Foo *pfoo = &foo;foo.a_ = 1;foo.b_ = 2;foo.to_str();pimC = &Foo::a_;        //指向類int成員的賦值cout<< pimC <<",int成員指標的內容:" << foo.*pimC<<",PFoo->int:" << pfoo->*pimC <<endl;foo.*pimC = 110;foo.to_str();cout<<pfab<<",get sum ab:"<<(foo.*pfab)()<<",指標:"<< ((pfoo->*pfab)()) <<endl;pimC = &Foo::b_;        //指向類int成員的賦值cout<< pimC << ",int成員指標的內容:" << foo.*pimC<<",PFoo->int:" << pfoo->*pimC <<endl;foo.*pimC = 220;foo.to_str();cout<<pfab<<",get sum ab:"<<(foo.*pfab)()<<",指標:"<< ((pfoo->*pfab)()) <<endl;/*typedef union{char ch;int i;} U;U u;u.i = 1;cout<<(int)u.ch<<endl;*//*int  i = 10;void * pv1 = &i;cout<< *(int *)pv1<<endl;*/// PF pf2 = fl;// PF pf3 = fc;//    pf1(1);    return 0;}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.