简体   繁体   English

具有默认参数的initializer_list构造函数

[英]initializer_list constructor with default argument

I define a initializer list ctor (sequence ctor) in a class and give it a default argument like this: 我在一个类中定义了一个初始化列表ctor(序列ctor),并给它一个默认参数,如下所示:

class Box 
{
public:
    Box(std::initializer_list<XMFLOAT3> vertices = {XMFLOAT3(), XMFLOAT3(), XMFLOAT3(), XMFLOAT3(), XMFLOAT3(), XMFLOAT3(), XMFLOAT3(), XMFLOAT3()});
    ~Box();
    void SetVertices(std::initializer_list<XMFLOAT3> vertices);
    XMFLOAT3 (&GetVertices())[8];
    Mesh &GetMesh() { return mMesh; }
private:
    XMFLOAT3 mVertices[8];
    Mesh mMesh;
};

but when I put an object of type Box inside another class the compiler complains that there's no default ctor available. 但是,当我将Box类型的对象放在另一个类中时,编译器会抱怨没有可用的默认ctor。 Why? 为什么?

EDIT if I call the default ctor into the containing class ctor's initialization list: 编辑如果我调用默认的构造函数到包含类构造函数的初始化列表:

Bone::Bone(std::string const &name) : mName(name), mCollisionBox{}
{
}

(I understand that when an initializer_list ctor is present the brace notation calls the default ctor first, not the initializer_list ctor with an empty list) it calls the default ctor. (我知道,当存在initializer_list ctor时,花括号符号会首先调用默认ctor,而不是带有空列表的initializer_list ctor)它将调用默认ctor。

The default ctor is also called if I explicitly call it: 如果我显式调用默认的ctor,也会调用它:

Bone::Bone(std::string const &name) : mName(name), mCollisionBox()
{
}

I'm using VisualC++ with Visual studio 2017 that's really strange.. 我在Visual Studio 2017中使用VisualC ++,这真的很奇怪..

EDIT 2 编辑2

in this example it works, but as I pointed out on the commented line it doesn't in VS 2017 在此示例中它有效,但是正如我在注释行中指出的那样,它在VS 2017中不起作用

http://coliru.stacked-crooked.com/a/e1de3b215c6c4634 http://coliru.stacked-crooked.com/a/e1de3b215c6c4634

You are asking the wrong question - because it is perfectly legal what you are asking for. 您提出的问题是错误的-因为您的要求完全合法。 Could be the error is something else or you need to provide a minimal and complete example. 错误可能是其他原因,或者您需要提供一个最小而完整的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM