I have always wondered why we code
virtual void MyFunction() = 0;
and not
pure virtual void MyFunction();
Is there a reference for the basis of this decision?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityI have always wondered why we code
virtual void MyFunction() = 0;
and not
pure virtual void MyFunction();
Is there a reference for the basis of this decision?
From The Design and Evolution of C++ - Bjarne Stroustrup - Addison-Wesley (ISBN 0-201-54330-3) - chapter 13.2.3:
The curious
= 0
syntax was chosen over the obvious alternative of introducing a new keywordpure
orabstract
because at the time I saw no chance of getting a new keyword accepted. Had I suggestedpure
, 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 overpure
, I used the tradition C and C++ convention of using 0 to represent not there.
Anyway looking at the C++ standard (§ 9.2 - Class members) = 0
is called pure-specifier.
pure
a contextual keyword you put at the end of a function definition, so virtual void MyFunction() pure
instead of = 0
, to go along with final
and override
.