小编Glo*_*tre的帖子

对使用'typedef'声明的函数使用'override'

C++ 11为函数引入了'override'说明符,我发现它很有用,因为它明确表示正在覆盖虚函数.但是,我似乎无法使用typedef声明的函数.

我知道'覆盖'不是关键字,它与它有关吗?

以下代码说明了我的观点:

#include <iostream>

typedef char ReturnsChar();

class Basic
{
    public:
    virtual char get_a();
    virtual ReturnsChar get_z;
};

char Basic::get_a() { return 'a'; }
char Basic::get_z() { return 'z'; }

class Capitalized : public Basic
{
    public:
    // Can override explicitly if I use the normal definition
    char get_a() override;

    // Compiles if I use the typedef but not 'override'
    ReturnsChar get_z;

    // Will not compile, but would like to do this
    //ReturnsChar get_z override; 

};

char Capitalized::get_a() …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11 c++14

16
推荐指数
1
解决办法
854
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

language-lawyer ×1