zoukankan      html  css  js  c++  java
  • 指向类成员的指针并非指针

    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_ab
    	
    	Foo 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;
    }
    

      

  • 相关阅读:
    QString::toStdString() crashes
    Consolas 字体
    Mesh BRep Shapes
    PyOpenCL库安装
    全国精确到乡镇的行政边界、路网水系建筑poi等矢量shp免费下载
    DEM数据(ASTER GDEM|SRTM|GLS2005|ALOS DEM)下载
    IDL基础
    辐射定标与FLAASH大气校正
    Circos绘图—基础
    R-散点密度图
  • 原文地址:https://www.cnblogs.com/wucg/p/2807167.html
Copyright © 2011-2022 走看看