C ++ 11使用新实例参数委托构造函数?

时间:2013-01-16 21:41:39

标签: c++ c++11 constructor visual-studio-2012 delegation

在使用Visual Studio 2012年11月CTP C ++编译器进行编译时遇到问题...只是想确保我没有遗漏任何明显的东西。

谢谢!

编辑:删除了标题,使其更简单。

class Location
{
public:
    Location();
};

class Shape
{
public:
    Shape();
    Shape(Location location);
};


// Doing this by pointer works ...
// Shape::Shape(Location* location){}
// Shape::Shape() : Shape(new Location()){}

Shape::Shape(Location location)
{
}

Shape::Shape()
    : Shape(Location())
    // error C2143: syntax error: missing ';' before ':'
{
    // int x = 0;
    // (void) x;  // Added these two lines in some cases to get it to compile.
    // These two lines do nothing, but get around a compiler issue.
}

1 个答案:

答案 0 :(得分:2)

// .h Simplification
class Location
{
public:
  Location() {}
  Location(Location const& other) {}
};

class Shape
{
  Shape();
  Shape(Location location);
};

// How about by value or reference?
Shape::Shape(Location location)
{
}

Shape::Shape(void)
  : Shape(Location()) // error C1001: An internal error has occurred in the compiler.
{
}

int main() {}

以上代码在gcc 4.7.2

中编译并运行

我必须对您的代码进行一些更改才能使其编译。在简化事物时,尽量保持简化代码的编译。 http://sscce.org/