zoukankan      html  css  js  c++  java
  • 类成员函数指针的特殊之处

    下面讨论的都是类的非静态成员函数。

    类成员函数指针的声明及调用:

    //pr是指向Base类里的非静态成员函数的指针
    //其行参为(int, int),返回值为void
    void (Base::*pr)(int, int);
    
    //需通过对象调用
    //object是Base类的一个对象或指针(可多态)
    ( object->*pr)( _r, _c)

    而其实质和普通的指针也有区别:

    //下面是隐藏的代码是相关类型的定义

    void Foo1(int a, int b, int c)
    {
    	return;
    }
    
    void Foo2(int a, int b)
    {
    	return;
    }
    
    class Base{
    public:
    	void Foo3(int a, int b, int c)
    		{return;}
    	void Foo3(int a,int b)
    		{return;}
    };
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	void (Base::*p1)(int,int,int) = &(Base::Foo3);
    	void (Base::*p2)(int,int) = &(Base::Foo3);
    	void (Base::*p3)(int,int,int) = NULL;
    	cout<<"Foo1: "<<Foo1<<'\t'<<sizeof(&Foo1)<<'\n'
    		<<"Foo2: "<<Foo2<<'\t'<<sizeof(&Foo2)<<'\n'
    		<<"p1:   "<<p1<<'\t'<<sizeof(p1) <<'\n'
    		<<"p2:   "<<p2<<'\t'<<sizeof(p2) <<'\n'
    		<<"p3:   "<<p2<<'\t'<<sizeof(p3) <<endl;
    }

    输出结果为:

    Foo1: 00BB1510  4
    Foo2: 00BB1520  4
    p1:   1 4
    p2:   1 4
    p3:   0 4

    从结果上来看,指向成员函数的指针值均为1。且在调试过程中可以看到:

    p1    error: cannot obtain value    void*

    p2    error: cannot obtain value    void*

    p3    error: cannot obtain value    void*

    但这个指针指向的函数实际上是可以被调用的。

    搜索之,看到了一篇文章:

    http://blog.csdn.net/hairetz/archive/2009/05/06/4153252.aspx

    1。成员函数指针不是指针。从代码看出,在main函数的调用栈(calling stack)中首先依次压入四个成员函数指针,如果它们是普通指针的话,它们之间的偏移量应该是4个字节,可是实际的情况却是这样的:

    ”The implementation of the pointer to member function must store within itself information as to whether the member function to which it refers is virtual or nonvirtual, information about where to find the appropriate virtual function table pointer (see The Compiler Puts Stuff in Classes [11, 37]), an offset to be added to or subtracted from the function's this pointer (see Meaning of Pointer Comparison [28, 97]), and possibly other information. A pointer to member function is commonly implemented as a small structure that contains this information, although many other implementations are also in use. Dereferencing and calling a pointer to member function usually involves examining the stored information and conditionally executing the appropriate virtual or nonvirtual function calling sequence.“

    这篇文章较深入地研究了成员函数指针,及比较了普通成员函数指针和虚函数指针在转化的过程中存在那些差异。

  • 相关阅读:
    MVC vs SVC
    迪米特原则与接口隔离原则
    依赖倒置原理:依赖、稳定的抽象层
    【调侃】IOC前世今生
    Visual SVN Server启动失败0x8007042a错误
    syntax error, unexpected '['
    navicat将多个表导出为一个sql文件
    我的flashfxp左右界面怎么变成这样了?
    什么是国际顶级域名?
    什么是A记录/CNAME记录/MX记录/TXT记录
  • 原文地址:https://www.cnblogs.com/h46incon/p/2029718.html
Copyright © 2011-2022 走看看