C++ member function pointer
2015-05-07 16:37
351 查看
//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
//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 strict class 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; }
相关文章推荐
- C++ Member Function Pointer
- C++ 成员函数指针的问题 pointer-to-member-function
- C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别
- C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别
- C++: member function pointer for SomeClass
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
- C++对象模型——指向Member Function的指针 (Pointer-to-Member Functions)(第四章)
- 深度探索c++对象模型之member function的具现行为
- C++ Function Pointer
- 深度探索C++对象模型---Function语义学之Member的各种调用方式
- 深度探索C++对象模型---Member Function的各种调用方式
- C++ - 成员函数(member function)模板(template) 详解 及 代码
- Member Function Pointers and the Fastest Possible C++ Delegates
- Member Function Pointers and the Fastest Possible C++ Delegates
- 【c++】cannot declare member function *** to have static linkage [-fpermissive]
- C++ - 成员函数(member function)模板(template) 详解 及 代码
- C++'s Function pointer and C#'s delegate
- Is the type of “pointer-to-member-function” different from “pointer-to-function”?
- experiment : use class member function pointer
- Pointer to a member function