C++丛林历险记之 reference to member

类别:编程语言 点击:0 评论:0 推荐:

       不好意思,因为不能登陆CSDN论坛,只好把回帖放到这里来了。这个帖子讨论的是在C++中如何声明 reference to member function。我查阅了C++标准,在C++中没有reference to member类型。下面是回帖:

       C++ 中有pointer to member,但是没有reference to member。为什么没有呢?我不知道,我所知道是pointer to member跟普通的指针有非常大的分别,严格地讲它并不是指针。pointer to member function 既可以指向非虚成员函数,也可以指向虚成员函数,这里有两种实现的方法:第一种是在pointer to member function中保存成员函数的绝对地址,对非虚成员函数和虚成员函数一视同仁;第二种是区别对待非虚成员函数和虚成员函数,对于非虚成员函数,同方法一,对于虚成员函数,在pointer to member function中保存成员函数在虚表中的偏移量。Dr. Stroustrup 在TCPL中似乎推荐的是第二种方法,我摘录一段:

   Naturally, if we knew which member we wanted to call we would invoke it directly rather than mess with pointers to members. Just like ordinary pointers to functions, pointers to member functions are used when we need to refer to a function without having to know its name. However, a pointers to member isn't a pointer to a piece of memory the way a pointer to a variable or a pointer to a function is. It is more like an offset into a structure or an index into an array. When a pointer to members is combined with a pointer to an object of the right type, it yields something that identifies a particular member of a particular object.


   This can be represented graphically like this:
 
                                    vtbl:
                   ---------    s  ------         ------------------------
       p ----->|      ----|---->|   ---|----->|       X::start            |
                  |           |   V  |     \|         ------------------------
                  |           |       |      |\
                   ---------       |      |  \
                                     |      |    \   -------------------------
                                     |      |       |      X::suspend          |
                                     |      |        -------------------------
                                     |      |
                                     ------
                                      
   Because a pointer to a virutal member(s in this example)is a kind of offset, it does not depend on an object's location in memory. A pointer to virtual member can therefore safely be passed between different address spaces as long as the same object layout is used in both. Like pointers to ordinary functions, pointers to non-virtual member functions cannot be exchanged between address spaces.
  
    下面让我们看看现实中不同的编译器是如何处理pointer to member类型的吧。老实说,测试的结果很有戏剧性,出乎我的预料之外。


////////////////测试程序//////////////////////////

#include <iostream>
#include <cstdlib>

template<typename _T>
     void print_memory(const _T& __v)
     {
           for(int i=0;i<sizeof(_T);++i){
           std::cout.width(2);
           std::cout.fill('0');
           std::cout << std::hex << static_cast<unsigned int>(reinterpret_cast<const unsigned char*>(&__v)[i]) << ' ';
     }
 }

struct foo{
         typedef void (foo::*pointer_mem_fun)();
         virtual void vhello(){std::cout<<"virtual hello world\n";}
         void hello(){std::cout<<"hello world\n";}
         virtual void vhello2(){std::cout<<"virtual hello world 2\n";}
         void hello2(){std::cout<<"hello world 2\n";}
         virtual void vhello3(){std::cout<<"virtual hello world 3\n";}
         void hello3(){std::cout<<"hello world 3\n";}
};

void call(foo::pointer_mem_fun p)
{
        print_memory(p);
        (foo().*p)();
}

int main()
{
       call(&foo::hello);
       call(&foo::hello2); 
       call(&foo::hello3); 
       call(&foo::vhello);
       call(&foo::vhello2);
       call(&foo::vhello3);
       system("pause");
}

////////////////测试结果//////////////////////////

Microsoft Visual C++ 6.0

2e 14 40 00 hello world
56 14 40 00 hello world 2
3d 14 40 00 hello world 3
33 14 40 00 virtual hello world
47 14 40 00 virtual hello world 2
51 14 40 00 virtual hello world 3

Dev-C++ 4.9.5.0

00 00 ff ff bc 10 41 00 hello world
00 00 ff ff 74 10 41 00 hello world 2
00 00 ff ff 98 10 41 00 hello world 3
00 00 02 00 00 00 22 00 virtual hello world
00 00 03 00 00 00 22 00 virtual hello world 2
00 00 04 00 00 00 22 00 virtual hello world 3

VisualAge C++ Professional / C for AIX Compiler, Version 5
 
20 00 13 b0 00 00 00 00 ff ff ff ff ff ff ff ff hello world
20 00 13 bc 00 00 00 00 ff ff ff ff ff ff ff ff hello world 2
20 00 13 c8 00 00 00 00 ff ff ff ff ff ff ff ff hello world 3
00 00 00 00 00 00 00 08 ff ff ff ff ff ff ff ff virtual hello world
00 00 00 00 00 00 00 10 ff ff ff ff ff ff ff ff virtual hello world 2
00 00 00 00 00 00 00 18 ff ff ff ff ff ff ff ff virtual hello world 3

你能看懂这个金字塔吗?我的推理结果是:VC使用的是前述的第一种方法,Dev-C++ 和 VisualAge C++使用的前述的第二种方法。

本文地址:http://com.8s8s.com/it/it29523.htm