31

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?

  • 2
    No opinions? Ok then. Because that's how the C++ language is defined. – gnasher729 May 20 '15 at 15:20
  • 4
    Note that a "pure" function often means something unrelated. – immibis May 20 '15 at 19:36
71

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

Anyway looking at the C++ standard (§ 9.2 - Class members) = 0 is called pure-specifier.

  • 11
    that's cool. Reminds me of why PHP has so many different ugly function names: because the first interpreter used strlen as hash function and distribution should be good (i-programmer.info/news/98-languages/…) – Aitch May 20 '15 at 9:21
  • 10
    The introduction of context-sensitive keywords in C++11 has reduces the damage that adding keywords causes. It should be possible to make 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. – Yakk May 20 '15 at 13:56
  • 21
    As an aside, nearly everywhere a "pure function" is understood to be a function whose output only depends on its input, and which has no side-effects. – Deduplicator May 20 '15 at 15:33
  • 2
    Pretty much any question of "why does C++ do X?" can be answered by that book. It should be required reading for any serious C++ programmer. – Steven Burnap May 20 '15 at 15:39
  • 4
    @Random832 it's "pure virtual" (as in "100% virtual and 0% anything else"), not just "pure". – immibis May 20 '15 at 19:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.