C++ member function pointer
来源:互联网 发布:什么是云计算和大数据 编辑:程序博客网 时间:2024/04/28 18:06
//1.derived class can call the base class function, but the base class can not call the derived class function, member pointer can not do it
//2.c++ member function is strict
//3.the size of C++ pointer maybe not equal to the size of machine word
#include <stdio.h>#include <iostream>using namespace std;//1.0--------------------------------------------------------------------------------------//1.derived class can call the base class function, but the base class can not call the derived class function, member pointer can not do it//2.c++ member function is strictclass base{ int a;public: void hello() { cout<<"base hello\n"; } void world() { cout<<"base world\n"; } };class Derived1: public base{public: void hello() { cout<<"Derived1 hello\n"; } void call(Derived1 *b1, void (Derived1::*fun)()) { (b1->*fun)(); }};class Derived2:public base{public: void world() { cout<<"Derived2 world\n"; }};/*void call(base *b1, void (base::*fun)()) //can not work{ (b1->*fun)();}*/void call(Derived1 *b1, void (Derived1::*fun)()){ (b1->*fun)();}typedef void (Derived1::*pFun)(); //1.1--------------------------------------------------------------------------------------class BaseClass { public: void BaseClassFun(int x, char *p) { printf("In BaseClass\n"); };};class DerivedClass : public BaseClass { public:};//1.1--------------------------------------------------------------------------------------class CBase1 {};class CBase2 {};class CDerive2 : public CBase1, public CBase2 {};typedef void (CDerive2::*FPderive2)();int main() { // Declare a member function pointer for BaseClass typedef void (BaseClass::*m_f_p)(int, char *); m_f_p memfunc_ptr; memfunc_ptr = &DerivedClass::BaseClassFun; DerivedClass *dc; char pstr[]=""; ( dc->*memfunc_ptr)(1,pstr);//--------------------------------------------------------//3.the size of C++ pointer maybe not equal to the size of machine word void (*pf)(); cout << "sizeDerive2 = " << sizeof(pf) << endl; size_t sizeDerive2 = sizeof(FPderive2); cout << "sizeDerive2 = " << sizeDerive2 << endl;//---------------------------------------------------------// typedef void (base::*pFun)(); Derived1 d1; Derived2 d2; pFun pf1; pf1=&Derived1::hello; (&d1)->call(&d1,&Derived1::hello); // (&d1)->call(&d1,&Derived1::world);// (&d1)->call(&d1,&base::hello);// (&d1)->call(&d1,&base::world); call(&d1,&base::world); call(&d1,&Derived1::hello); call(&d1,&Derived1::world); call(&d1,&base::hello); call(&d1,&base::world); return 0;}
0 0
- member function pointer
- C++ member function pointer
- Function Pointer of class member function C++的类成员函数指针简要
- pointer to function and pointer to member function
- experiment : use class member function pointer
- Const Member Function(C++)
- Relationship of the this pointer and (non-)const member function
- C++ 成员函数指针的问题 pointer-to-member-function
- Is the type of “pointer-to-member-function” different from “pointer-to-function”?
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
- Function Pointer of class member function (类的成员函数指针)
- Function Pointer of class member function (类的成员函数指针)
- Pointer that points to function (C)
- C Programming Language: Function Pointer Array Study
- pointer to member
- Function Pointer
- Function Pointer
- Error c3876: function call missing argument list; use '' to create a pointer to member
- Theme设置,解决应用启动中的黑白屏现象
- 远程Java多进程调试方法
- java 对象序列化工具类
- linux下定时自动重启tomcat服务器
- hibernate 配置多个数据源
- C++ member function pointer
- java字符串的各种编码转换
- 黑马程序员 —— Java基础
- 卡尔曼滤波器 – Kalman Filter
- 时间戳问题汇总
- ini_set改变upload_tmp_dir的问题
- 梯度下降法的数学推导
- 黑马程序员——java_(内省,javaBean,动态代理)
- afn2.0同步和异步 及读取json格式