关闭

(deriveObj.*pVIDeriveMemFunc)(20);

322人阅读 评论(0) 收藏 举报
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

class Base {
public:
    //ordinary member function
    void setValue(int iValue){printf("Base::setValue(%d)\n",iValue);m_iValue=iValue; }
	
    //virtual member function
    virtual void dumpMe(){printf("Base::dumpMe()\n");}
    virtual void foobar(){printf("Base::foobar()\n");}
	
protected:
    int m_iValue;
};

class Derived:public Base{
public:
    //ordinary member function
    void setValue(int iValue){Base::setValue(iValue);}
	
    //virtual member function
    virtual void dumpMe(){printf("Derived::dumpMe()\n");}
    virtual void foobar(){printf("Derived::foobar()\n");}
private:
    double m_fValue;
};    
typedef void (Base::*PVVBASEMEMFUNC)(void);
typedef void (Derived::*PVVDERIVEMEMFUNC)(void);
typedef void (Base::*PVIBASEMEMFUNC)(int);
typedef void (Derived::*PVIDERIVEMEMFUNC)(int);

int main(int argc, char* argv[])
{	
    PVIBASEMEMFUNC pVIBaseMemFunc = &Base::setValue;
	PVIDERIVEMEMFUNC pVIDeriveMemFunc = static_cast<PVIDERIVEMEMFUNC>(pVIBaseMemFunc);
	
    PVVBASEMEMFUNC      pVVBaseMemFunc   = &Base::foobar;
    PVVDERIVEMEMFUNC pVVDeriveMemFunc = static_cast<PVVDERIVEMEMFUNC>(pVVBaseMemFunc);
	
    Base baseObj;
    (baseObj.*pVIBaseMemFunc)(10);
    (baseObj.*pVVBaseMemFunc)();
	
    Derived deriveObj;
    (deriveObj.*pVIDeriveMemFunc)(20);
    (deriveObj.*pVVDeriveMemFunc)();
	
    return 0;
}
/*
Base::setValue(10)
Base::foobar()
Base::setValue(20)
Derived::foobar()
Press any key to continue

*/

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2989316次
    • 积分:40258
    • 等级:
    • 排名:第93名
    • 原创:399篇
    • 转载:4555篇
    • 译文:5篇
    • 评论:609条
    文章分类
    最新评论