Just wondering why the syntax for virtual functions uses a const before the curly braces, as below:
virtual void print(int chw, int dus) const;
Incidentally, the code doesnt seem to work without the const, which is interesting.. not sure why?
many thanks!
The
constin the function signature signifies a const member function – Anthony Williams gave a great answer on the implications.Note that there is nothing special about virtual member functions functions in that regard, constness is a concept that applies to all non-static member functions.
As for why its not working without – you can’t call non-const members on a const instance. E.g.: