C++中怎样获取类成员的指针



我们都知道C++ class中有三种成员函数,static(静态的),nonstatic(非静态的),virtual(虚拟的)各种成员函数的指针各有差别,下面是一个完整的样例:(注意红颜色的差别)

class A

{

public:

static void staticmember(){cout<<"static"<<endl;}   //static member

void nonstatic(){cout<<"nonstatic"<<endl;}          //nonstatic member

virtual void virtualmember(){cout<<"virtual"<<endl;};//virtual member

};

int main()

{

A a;

//static member,取得的是该函数在内存中的实际地址,并且由于static成员是全局的,所以不能用A::限定符

void (*ptrstatic)()= &A::staticmember;

//nonstatic member 取得的是该函数在内存中的实际地址

void (A::*ptrnonstatic)() = &A::nonstatic;

//虚函数取得的是虚函数表中的偏移值,这样能够保证能过指针调用时相同的多态效果

void (A::*ptrvirtual)() = &A::virtualmember;

//函数指针的使用

ptrstatic();

(a.*ptrnonstatic)();

(a.*ptrvirtual)();

}

时间: 2024-08-26 20:39:02

C++中怎样获取类成员的指针的相关文章

C++中如何获取类成员的指针

 我们都知道C++ class中有三种成员函数,static(静态的),nonstatic(非静态的),virtual(虚拟的)各种成员函数的指针各有区别,以下是一个完整的例子:(注意红颜色的区别) class A { public: static void staticmember(){cout<<"static"<<endl;}   //static member void nonstatic(){cout<<"nonstatic&

C/C++ 类成员函数指针 类成员数据指针

普通函数指针:  "return_type (*ptr_name)(para_types) " 类成员函数指针: "return_type (class_name::*ptr_name)(para_types)" 类数据成员指针: "type class_name::* ptr_name"; C/C++: 1 class Demo 2 { 3 public: 4 Demo():data(100) 5 { 6 7 } 8 int data; 9 i

函数指针与类成员函数指针

1,函数指针函数指针,顾名思义就是函数的指针,而指针其实就是地址,那么函数指针就是存储函数的地址,可是实际大部分时间里,我们写程序时是根本不会去考虑函数地址在哪里.我们只需要知道函数原型和函数声明就可以.但是想象一下,我们总是以为函数就应该接收参数,那么函数本身是否可以作为函数的参数呢?我做了下面的一个实验 #include<iostream>#include<stdio.h>#include<cstring>using namespace std;typedef in

C++的类成员和类成员函数指针

类成员函数指针: 用于访问类成员函数,和一般函数指针有区别. 类成员函数处理的是类数据成员,声明类成员函数指针的同时,还要指出具体是哪个类的函数指针才可以.调用时也要通过对象调用. 而对于类的静态成员函数,它是类对象共享的,且只能处理静态数据成员,所以它的函数指针可以类似一般函数指针一样使用. 1 class Test 2 { 3 public: 4 void fun(int); 5 void fun(int) const; //重载函数,加一个const限制 6 static void fun

让类成员函数指针成为可调用对象

类成员函数指针实践上是一个指针类型,不可直接通过调用运算符()作为可调用对象调用,一般调用该类成员函数指针需要指定该指针对应的对象. 一般情况下调用类成员函数指针: // a.h #ifndef A_H #define A_H #include <iostream> using std::cout; using std::endl; class A{ public:     void print(); }; #endif // a.cpp #include "a.h" vo

C++ 获取类成员函数地址方法 浅析

C语言中可以用函数地址直接调用函数: void print () { printf ("function print"); } typdef void (*fun)(); fun f = print; f(); C++中类非静态成员函数必须通过实例去调用,C++中类成员函数调用: class test { public: void print () { printf ("function print"); } }; 我们同样可以通过定义函数指针来调用如下: type

类成员的指针

ps:const对象只能调用const函数!!非const对象随便!! 成员指针只应用于类的非 static 成员.static 类成员不是任何对象的组成部分,所以不需要特殊语法来指向 static 成员,static 成员指针是普通指针. int *p = &Screen::total;    (total 是 static int total;) 例子: #include<iostream> using namespace std; class Screen{ public: st

获取VB类模块成员函数指针(转)

最近在做一些VB6.VBA的项目,被如何获取类模块中的函数指针这个问题所困扰,收集整理后,有2分资料值得收藏,特将关键部分留存,以备后续查找. 参照连接1:http://www.cnblogs.com/pctgl/articles/1352916.html 参照连接2:http://blog.csdn.net/lyserver/article/details/4224676 以下是链接1中的部分内容: 1. 函数地址 = GetClassProcAddress ( 指定为哪个函数 [上面解释],

C++ Primer 学习笔记_103_特殊工具与技术 --类成员指针

特殊工具与技术 --类成员指针 成员指针可以做到:获得特定成员的指针,然后从一个对象或别的对象获得该成员.成员指针应该包含类的类型以及成员的类型. 一.声明成员指针 测试类: class Screen { public: typedef std::string::size_type index; char get() const; char get(index ht,index wd) const; private: std::string contents; index cursor; ind