为什么纯虚函数初始化为0

为什么纯虚函数初始化为0

本文介绍了为什么纯虚函数初始化为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。这在他的书,第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:

他还明确指出不需要设置将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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:10