为什么纯虚函数由0初始化? [英] Why pure virtual function is initialized by 0?

查看:441
本文介绍了为什么纯虚函数由0初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们总是声明一个纯虚函数:

We always declare a pure virtual function as :

virtual void fun () = 0 ;

它总是被分配到0.

我知道这是将这个函数的vtable条目初始化为NULL,这里的任何其他值导致编译时错误。请告诉我这种理解是否正确。

What I understand is that this is to initialize the vtable entry for this function to NULL and any other value here results in a compile time error. Please tell me whether this understanding is correct or not.

推荐答案

原因 = 0 是因为Bjarne Stroustrup没有想到他可以得到另一个关键字,例如纯,通过C ++社区在实现该功能时。这在他的书 The Design& C ++的演变,第13.2.3节:

The reason =0 is used is that Bjarne Stroustrup didn't think he could get another keyword, such as "pure" past the C++ community at the time the feature was being implemented. This is described in his book, The Design & Evolution of C++, section 13.2.3:


选择了curious = 0语法...
在我看来没有机会
获得接受的新关键字。

The curious =0 syntax was chosen ... because at the time I saw no chance of getting a new keyword accepted.

他还明确表示,这不需要设置vtable条目为NULL,这样做不是实现纯虚拟函数的最佳方式。

He also states explicitly that this need not set the vtable entry to NULL, and that doing so is not the best way of implementing pure virtual functions.

这篇关于为什么纯虚函数由0初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆