【问题标题】:C++0x Peer Constructor in VC2010VC2010 中的 C++0x 对等构造函数
【发布时间】:2010-10-03 02:24:31
【问题描述】:

根据C++0x spec,以下是合法的

class A {
    A(int i) : x(i) {}
    A() : A(0) {}
    int x;
};

但它在 VC 2010 中无法编译 ("A" is not a nonstatic data member or base class of class "A")。有人知道出了什么问题吗?

【问题讨论】:

标签: c++ constructor c++11


【解决方案1】:

在撰写本文时,Visual C++ 2010(也称为 VC++ 10.0)不支持委托构造函数,而这是您的代码 sn-p 所需要的。 VC++ 10.0 仅部分支持 C++0x,在撰写本文时,还没有编译器实现整个 C++0x 功能集(尽管很快就会改变,尤其是在 C++0x 标准最终确定后)。

斯科特迈耶斯有a summary of C++0x support in gcc and MSVC compilers。这是C++0x feature support in different compilers 的另一个列表。另外,a list of C++0x features supported in Visual C++ 2010 straight from the horse's mouth

现在,直接在构造函数的初始化列表中初始化所有成员:

class A
{ 
public:
    A(int i) : x(i) {} 
    A() : x(0) {} 
private:
    int x; 
};

【讨论】:

  • @jameszhao00:它“有效”,因为这不是委托构造函数。您正在创建一个未命名的临时 A,一旦构造函数完成,它将被销毁,这不是您想要的。
  • 好点!谢谢你的澄清。太习惯了 Java/C# 我猜 :(
【解决方案2】:

Visual Studio 还不支持所有的 0x。 (不应该期望没有人会这样做;0x 尚未最终确定。)

This describes what 0x features are implemented in VS 2010.

【讨论】:

    【解决方案3】:

    MSVC++ 2010 不支持delegating constructor

    This page 列出了 C+ 0x 特性及其在流行编译器中的支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-08
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2013-09-23
      • 2023-03-07
      相关资源
      最近更新 更多