2

I'm a beginner in C++. I don't quite understand the following declaration, especially "const = 0" part. Could someone please explain it? Thank you! And what kind of grammar does this belong to and how can I learn it?

class Expression
{
public:
     virtual double evaluate() const = 0;
};
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
James Jian
  • 41
  • 3
  • `const` and `= 0` are two orthogonal properties of that definition. – Daniel Langr Jun 01 '20 at 12:18
  • 2
    Const is you can call it on a const object, = 0 means it's an abstract declaration. A c++ book is the best place to learn it. – user1937198 Jun 01 '20 at 12:18
  • 5
    Does this answer your question? [c++ : code explanation for method prototype with const = 0](https://stackoverflow.com/questions/21187965/c-code-explanation-for-method-prototype-with-const-0) – snaksa Jun 01 '20 at 12:18
  • The best way to learn it is with a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Eljay Jun 01 '20 at 12:19
  • 2
    In C++, a virtual method is one a derived class may re-implement. It might have an implementation or default implementation in the base class. The '= 0' part, which makes the method pure virtual, means the derived class can't be instantiated without implementing the method. The const part means the method does not change any data members, unless marked mutable, and therefore may be called on a const instance of the class. This is independent of the method being virtual. – Uri Raz Jun 01 '20 at 12:24
  • @Bathsheba, BTW, do you know what the motivation behind this syntax was? – Evg Jun 01 '20 at 12:25
  • 1
    @Evg I don't know for sure, but there was a desire to keep the number of new keywords to a minimum. `class` was introduced (in hindsight possibly a mistake) to facilitate CFront reduction of C++ code to C. – Bathsheba Jun 01 '20 at 12:27
  • @Evg Virtual functions are usually implemented with a virtual function table, which contains pointers to the class' functions. Derived classes' constructors & destructors overwrite the table. A '= 0' is like saying "put a null pointer in this function's entry in the virtual function table" to indicate it doesn't have any implementation. – Uri Raz Jun 01 '20 at 12:29
  • @UriRaz, but you can have an implementation of a pure virtual member function. – Evg Jun 01 '20 at 12:30
  • @Evg The standard disallows implementing a pure virtual member function. Visual Studio, which is not strict, allows it as a mixin - Visual Studio will not allow instantiating a class with implemented pure virtual function, but allows derived classes to call it. AFAIK, this non standard feature was made popular in one of Scott Meyer's books. – Uri Raz Jun 01 '20 at 12:36
  • @UriRaz: U wot? I'm pretty sure it's standard C++. See https://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation – Bathsheba Jun 01 '20 at 12:39
  • Thanks a lot! two associated questions at the top can answer this question perfectly. – James Jian Jun 01 '20 at 14:49
  • @Uri Raz does the derived class have to include *const* keyword? – James Jian Jun 01 '20 at 14:59

0 Answers0