分享
 
 
 

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

王朝c/c++·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

不好意思,因为不能登陆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++使用的前述的第二种方法。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有