C++ pure virtual const member function
From Microsoft Docs:
To declare a constant member function, place the
const
keyword after the closing parenthesis of the argument list.
So it should be:
virtual void print() const = 0;
Only the virtual void print() const = 0
form is acceptable. Take a look at the grammar specification in C++03 §9/2:
member-declarator:
declarator pure-specifieropt
declarator constant-initializeropt
identifieropt:
constant-expressionpure-specifier:
= 0
The const
is part of the declarator -- it's the cv-qualifier-seqopt in the direct-declarator (§8/4):
declarator:
direct-declarator
ptr-operator *declarator*direct-declarator:
declarator-id
direct-declarator(
parameter-declaration-clause)
cv-qualifier-seqopt exception-specificationopt
direct-declarator[
constant-expressionopt]
(
declarator)
Hence, the = 0
must come after the const
.
Of course you can. The correct syntax is:
virtual void print() const = 0;