167

We always declare a pure virtual function as:

virtual void fun () = 0 ;

I.e., it is always assigned to 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
  • 17
    Note that vtable is not a language requirement, but just an implementation option for virtual methods. A compiler could create the same abstraction with a different implementation (that is, without vtable, and without any element there being 0) – David Rodríguez - dribeas Jan 28 '10 at 17:55
  • @hype Re your supplementary question - that's what my answer (and several others) says. –  Jan 28 '10 at 18:34
  • 1
    Just curious, what happens if i give, `virtual void func() = 100;` – krithikaGopalakrishnan Jul 30 '19 at 04:58

11 Answers11

175

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:

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

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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
85

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:

The curious =0 syntax was chosen over the obvious alternative of introducing a new keyword pure or abstract because 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 =0 syntax 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. [ ... ]

1§13.2.3 Syntax

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
33

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

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.

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
  • 6
    +1 for referring to the C++ Standard. It's a kind of surprising that one of best possible answer and got only 1 vote so far. Reading the standard should be the very first step in order to solve C++ questions. – mloskot Jan 28 '10 at 18:14
  • 8
    @mloskot: maybe because it doesn't answer the OP's question, just restates the situation? – just somebody Feb 03 '10 at 21:26
  • 6
    @just somebody - It includes the standard citation which states what's the syntax of pure virtual function declaration and that the syntax uses pure-specifier `= 0` What else would you like to know? It would be the same as asking why function body is wrapped with {} The answer would be, because that's what C++ syntax defines. – mloskot Feb 03 '10 at 23:43
17

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cquillen
  • 1,287
  • 2
  • 9
  • 14
17

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.

The = 0 syntax 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.

sbi
  • 219,715
  • 46
  • 258
  • 445
  • 5
    +1 for explaining the disadvantage of introducing a new keyword. That's helpful to understand the rationale, but C++ syntax seems absurdly convoluted to me and I wish they would have made it more human-readable--a `pure` keyword would have been great in my book. Anyway, it's good to understand the rationale. – Keith Pinson Dec 21 '12 at 22:13
  • @KeithPinson If you want a pure keyword, you can `#define pure = 0`. – jdh8 Mar 26 '16 at 10:05
  • @jdh8: Ugh. Just... Ugh. – sbi Mar 27 '16 at 21:46
  • As a side note, in a class with Bjarne he said he really wanted to get the "pure" keyword into C++... but he tried to get it in very late in the cycle before the C++ compiler was going to be shipped (IIRC less than two weeks). Ostensibly, it didn't make it in. – ThePhD Mar 27 '16 at 21:49
  • @ThePhD: ISTR he also says this somewhere in D&E. (I'm too lazy to look it up, though.) – sbi Mar 27 '16 at 21:50
  • @sbi Jerry Coffin's answer quotes this – milleniumbug Mar 27 '16 at 21:54
  • @milleniumbug: Oh. `:-)` – sbi Mar 27 '16 at 21:57
11

C++ must have a way to distinguish a pure virtual function from a declaration of a normal virtual function. They chose to use the = 0 syntax. 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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    -0: when you have nothing substantial to say on your own, use an extensive quote (see Jerry Coffin's answer for +1 ;) – just somebody Feb 03 '10 at 21:25
7

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

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
3

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.

luke
  • 36,103
  • 8
  • 58
  • 81
2

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.

rui
  • 11,015
  • 7
  • 46
  • 64
2

The = 0declares a pure virtual function.

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

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).

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 4
    That's actually not true. There's nothing wrong with providing a *definition* for a pure virtual function. The only thing `= 0` does is makes the entire class abstract and prohibits *virtual* calls to pure functions. Non-virtual calls are still perfcectly OK, which is when the definition (if you provided one) is used. – AnT stands with Russia Jan 28 '10 at 17:58
  • Just look at the godbolt output. There isn't room for ambiguity or speculation. I'll take a look myself later – Lewis Kelsey May 27 '20 at 02:40
  • It seems that it replaces the entry with `__cxa_pure_virtual` https://arobenko.gitbooks.io/bare_metal_cpp/content/compiler_output/abstract_classes.html instead of Base::f() – Lewis Kelsey May 27 '20 at 03:09
1

Well, you can also initialize the vtable entry to point to an actual function"

 virtual void fun()
 {
     //dostuff()
 }

Seems intuitive that the vtable entry can either be defined to point nowhere (0) or to a function. Letting you specify your own value for it would probably result in it pointing to garbage instead of to a function. But that is why "= 0" is allowed and "= 1" is not. I suspect Neil Butterworth is right about why "= 0" is used at all

Brian
  • 25,523
  • 18
  • 82
  • 173
  • 1
    Even i had similar opinion but since many have quoted standards and Bjarne's comments on the same, we have minimal chances of argument :) – mukeshkumar Feb 02 '10 at 13:43