How can I declare a pure virtual member function that is also const? Can I do it like this?
virtual void print() = 0 const;
or like this?
virtual const void print() = 0;
How can I declare a pure virtual member function that is also const? Can I do it like this?
virtual void print() = 0 const;
or like this?
virtual const void print() = 0;
From Microsoft Docs:
To declare a constant member function, place the
constkeyword 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.
Try this:-
virtual void print() const = 0;