I'm following a book that teaches C++ according to the standard C++11. The book shows an example that marks a subclass as explicit
.
From what I've gathered, this will make the compiler check for errors that have to do with dynamic binding, and you must also explicitly use the override
keyword whenever you override a virtual function otherwise it won't work. I did set up a small program to test this but I got an error. And I've never seen examples of the keyword explicit
be used like that anywhere else anyway.
Has the book provided inaccurate information or have I just missed something? Should I just use the override
keyword on it's own instead?
class base {
public:
virtual void f() {
cout << "Called from base" << endl;
}
};
class derived explicit : public base {
public:
void f() override {
cout << "Called from derived" << endl;
}
};
Writing c++ -std=c++11 main.cpp -o main.exe
in the terminal gave me the following error:
error: expected unqualified-id
class derived explicit : public base {
^