English 中文(简体)
初始化者名单是否压制了建造者的呼声?
原标题:Does initializer list suppress the call to constructor?
  • 时间:2010-11-26 07:35:37
  •  标签:
  • c++

如果我宣布一个有违约构造的班级,并界定一个有初步设计人名单的班级,那么将要求违约施工人作出这一定义吗? 为什么有人被称作或没有被称作?

class Sample
{
// this should be any aggregate type in c++
};

int main()
{
  Sample s = {0};
  return 0;
}
最佳回答

标准说(8。5/14)

初始动力的特性如下。 目的地类型是标的类型或标注的类型,来源类型是初始表达器的类型。 原始器具被掩蔽或作为母体表示清单时,对来源类型未作界定。

If the destination type is a (possibly cv-qualified) class type: — If the class is an aggregate (8。5。1), and the initializer is a brace-enclosed list, see 8。5。1。 。

8。5。1/13

[Note: An aggregate array or an aggregate class may contain members of a class type with a user-declared constructor (12。1)。 Initialization of these aggregate objects is described in 12。6。1。 ]

Also 12。6。1/2 says

When an aggregate (whether class or array) contains members of class type and is initialized by a brace-enclosed initializer-list (8。5。1), each such member is copy-initialized (see 8。5) by the corresponding assignment-expression。 If there are fewer initializers in the initializer-list than members of the aggregate, each member not explicitly initialized shall be value-initialized (8。5)。

问题回答
  • In C++03, only aggregate classes may be initialized with curly braces, and an aggregate class may not have a user defined constructor
  • In C++0x, this syntax is supported for non aggregate types through initializer lists and calls the appropriate constructor (taking a std::initializer_list)

当你提供一种装饰的原始器时,该类所有成员均从被掩饰的原始器的相应表述中录出。

这种初始化只适用于无法拥有用户申报的建筑商的聚合物,因此压制编造者几乎是学术性的。

The corresponding constructor is called: http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists

在C++中,你只能先把POD(解释旧数据)设定为={0}(至少在C++0x之前)。 因此,将不要求违约的建筑商,因为这套建筑群集了起来。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签