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

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

问题描述

我们总是将纯虚函数声明为:

We always declare a pure virtual function as:

virtual void fun () = 0 ;

即,始终将其分配为0。

I.e., it is always assigned to 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. Is this understanding correct or not?

推荐答案

原因 = 0 是使用的原因是Bjarne Stroustrup认为在实现该功能时,他不能在C ++社区之外获得另一个关键字,例如 pure。这在他的书 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:


选择了好奇的= 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天全站免登陆