本书的示例在类声明中使用“ explicit”关键字。无效吗?

sam*_*sam 3 c++ class c++11

我正在看一本根据标准C ++ 11讲授C ++的书。本书显示了一个将子类标记为的示例explicit

根据我的收集,这将使编译器检查与动态绑定有关的错误,并且override每当您覆盖虚拟函数时,还必须显式使用关键字,否则它将无法正常工作。我确实设置了一个小程序来对此进行测试,但出现错误。而且我从未见过explicit在其他任何地方都可以像这样使用关键字的示例。

这本书提供的信息不正确还是我只是错过了什么?我应该单独使用override关键字吗?

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;
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ -std=c++11 main.cpp -o main.exe在终端上写给我以下错误:

error: expected unqualified-id 
class derived explicit : public base {
                   ^
Run Code Online (Sandbox Code Playgroud)

lub*_*bgr 6

explicit在任何标准中,使用这种方式都是无效的C ++。从cppreference开始explicit

1)指定构造函数或转换函数(自C ++ 11起)是显式的,即,不能将其用于隐式转换和复制初始化。
2)[...](特定于C ++ 20 ...)

...,最重要的是:

The explicit specifier may only appear within the decl-specifier-seq of the declaration of a constructor or conversion function (since C++11) within its class definition.

This does not cover the case you have shown from the book. Have a look at the curated list of books and consider switching to one of those.