C++ 为什么纯虚函数初始化为 0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2156634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:18:29  来源:igfitidea点击:

Why is a pure virtual function initialized by 0?

c++abstract-classpure-virtual

提问by mukeshkumar

We always declare a pure virtual function as:

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

virtual void fun () = 0 ;

I.e., it is always assigned to 0.

即,它始终分配为 0。

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?

我的理解是,这是为了将此函数的 vtable 条目初始化为 NULL,而此处的任何其他值都会导致编译时错误。这种理解正确与否?

回答by

The reason =0is 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是 Bjarne Stroustrup 认为他无法获得另一个关键字,例如在实现该功能时超越 C++ 社区的“纯”。这在他的书The Design & Evolution of C++ 中描述,第 13.2.3 节:

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

选择了奇怪的 =0 语法……因为当时我认为没有机会接受新的关键字。

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.

他还明确指出,这不需要将 vtable 条目设置为 NULL,并且这样做不是实现纯虚函数的最佳方式。

回答by Jerry Coffin

As with most "Why" questions about the design of C++, the first place to look is The Design and Evolution of C++, by Bjarne Stroustrup1:

与大多数关于 C++ 设计的“为什么”问题一样,首先要看的是Bjarne Stroustrup 的The Design and Evolution of C++ 1

The curious =0syntax was chosen over the obvious alternative of introducing a new keyword pureor abstractbecause at the time I saw no chance of getting a new keyword accepted. Had I suggested pure, Release 2.0 would have shipped without abstract classes. Given a choice between a nicer syntax and abstract classes, I chose abstract classes. Rather than risking delay and incurring the certain fights over pure, I used the tradition C and C++ convention of using 0 to represent "not there." The =0syntax fits with my view that a function body is the initializer for a function also with the (simplistic, but usually adequate) view of the set of virtual functions being implemented as a vector of function pointers. [ ... ]

=0选择了奇怪的语法而不是引入新关键字的明显替代方案,pure或者 abstract因为当时我认为没有机会接受新关键字。如果我建议pure,2.0 版会在没有抽象类的情况下发布。如果在更好的语法和抽象类之间进行选择,我选择了抽象类。与其冒着延迟的风险和招致某些争论 pure,我使用了传统的 C 和 C++ 约定,即使用 0 来表示“不存在”。该=0语法与配合我认为一个功能体为功能也与所述一组虚拟功能的(简单,但通常是足够的)视图被实现为函数指针的向量初始化。[...]

1§13.2.3 Syntax

1§13.2.3 语法

回答by Kristopher Johnson

Section 9.2 of the C++ standard gives the syntax for class members. It includes this production:

C++ 标准的第 9.2 节给出了类成员的语法。它包括这个生产:

pure-specifier:
    = 0

There is nothing special about the value. "= 0" is just the syntax for saying "this function is pure virtual." It has nothing to do with initialization or null pointers or the numeric value zero, although the similarity to those things may have mnemonic value.

价值没有什么特别之处。“= 0”只是表示“这个函数是纯虚拟的”的语法。它与初始化或空指针或数值零无关,尽管与这些事物的相似性可能具有助记值。

回答by cquillen

I'm not sure if there is any meaning behind this. It is just the syntax of the language.

我不确定这背后是否有任何意义。这只是语言的语法。

回答by sbi

C++ has always shied away from introducing new keywords, since new reserved words break old programs which use these words for identifiers. It's often seen as one of the language's strengths that it respects old code as far as possible.

C++ 一直避免引入新的关键字,因为新的保留字会破坏使用这些词作为标识符的旧程序。它通常被视为语言的优势之一,即尽可能尊重旧代码。

The = 0syntax might indeed have been chosen since it resembles setting a vtable entry to 0, but this is purely symbolic. (Most compilers assign such vtable entries to a stub which emits an error before aborting the program.) The syntax was mainly chosen because it wasn't used for anything before and it saved introducing a new keyword.

= 0可能确实已选择语法,因为它酷似设置虚函数表条目0,但是这纯粹是象征性的。(大多数编译器将这样的 vtable 条目分配给一个存根,该存根在中止程序之前发出错误。)之所以选择该语法,主要是因为它以前没有用于任何事情,并且节省了引入新关键字的时间。

回答by JaredPar

C++ must have a way to distinguish a pure virtual function from a declaration of a normal virtual function. They chose to use the = 0syntax. They could just have easily done the same by adding a pure keyword. But C++ is pretty loath to add new keywords and prefers to use other mechanisms to introduce features.

C++ 必须有办法区分纯虚函数和普通虚函数的声明。他们选择使用= 0语法。他们可以通过添加一个纯关键字轻松地完成相同的操作。但是 C++ 非常不愿意添加新的关键字,并且更喜欢使用其他机制来引入特性。

回答by AnT

Nothing is "initilaized" or "assigned" zero in this case. = 0in just a syntactical construct consisting of =and 0tokens, which has absolutely no relation to either initialization or assignment.

在这种情况下,没有什么是“初始化”或“分配”零的。= 0在一个由=0标记组成的语法结构中,它与初始化或赋值完全没有关系。

It has no relation to any actual value in "vtable". The C++ language has no notion of "vtable" or anythng like that. Various "vtables" are nothing more than just details of specific implementations.

它与“vtable”中的任何实际值都没有关系。C++ 语言没有“vtable”或类似的概念。各种“vtables”只不过是具体实现的细节而已。

回答by luke

I remember reading that the justification for the funny syntax was that it was easier (in terms of standards-acceptance) than introducing another keyword that would do the same thing.

我记得读过有趣的语法的理由是它比引入另一个可以做同样事情的关键字更容易(就标准接受而言)。

I believe this was mentioned in The Design and Evolution of C++ by Bjarne Stroustrup.

我相信 Bjarne Stroustrup 在 The Design and Evolution of C++ 中提到了这一点。

回答by Alexander Gessler

The = 0declares a pure virtual function.

= 0声明了一个纯虚函数

What is understand is that this is to initialize the vtable entry for this function to NULL and any other value here results in compile time error

可以理解的是,这是为了将此函数的 vtable 条目初始化为 NULL,并且此处的任何其他值都会导致编译时错误

I don't think that's true. It's just special syntax. The vtable is implementation-defined. No one says a vtable entry for a pure member must be actually zeroed upon construction (although most compilers handle vtables similar).

我不认为这是真的。这只是特殊的语法。vtable 是实现定义的。没有人说纯成员的 vtable 条目必须在构造时实际归零(尽管大多数编译器处理 vtable 类似)。

回答by rui

I would assume that this is just part of the C++ grammar. I don't think there are any restrictions to how the compilers actually implement this for a given specific binary format. You're assumption probably was right for the early day C++ compilers.

我认为这只是 C++ 语法的一部分。我认为对于给定的特定二进制格式,编译器实际上如何实现这一点没有任何限制。您的假设可能适用于早期的 C++ 编译器。