【问题标题】:Do invocations of std constructors need to be qualified?是否需要对 std 构造函数的调用进行限定?
【发布时间】:2012-07-06 14:17:00
【问题描述】:

std 构造函数的调用是否需要使用std:: 进行限定?

class whatever : public std::runtime_error
{
public:
    explicit whatever(const std::string& what) : runtime_error(what) {}
};                                            // ^ do I need std:: here?

它可以在我的编译器上运行,但我不确定这种行为是否是标准的。

【问题讨论】:

  • 基本上,您是在问是否可以无条件引用基类(不一定是std)?

标签: c++ exception inheritance constructor namespaces


【解决方案1】:

不,你没有。在 whatever 类的范围内查找初始化列表中的名称。此类范围包括在基类中声明的名称,并且基类的名称 (runtime_error) 被插入到 std::runtime_error 的范围中(这是所有类的标准行为)。

请注意,如果您使用的名称是实际类名的typedef,则这不起作用。您很容易被 std::istream 和朋友所吸引。 See here.

【讨论】:

  • 有关“injected-class-name”的定义,请参见 C++2003, §9.2。
  • @Robᵩ:谢谢,我手头没有标准。 :o
【解决方案2】:

初始化列表中不需要限定条件(老实说,我不知道那里是否允许限定条件),因为它是一个基础,可以通过类查找找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2013-10-22
    • 2018-06-19
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多