使用聚合初始化列表比构造函数有优势吗?
我是 C++ 新手,我有一个问题...
我尝试通过制作测试应用程序来回答这个问题...在调试中,B 类初始化生成较少的汇编代码,但在发布模式下,我真的不能比如说...它优化了初始化:(
假设我有两个类:
class A
{
public:
int a, b, c, d;
A(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d)
{
}
};
class B
{
public:
int a, b, c, d;
};
使用
B b = {1, 2, 3, 4}
而不是
A a = A(1, 2, 3, 4);
有什么优点吗?
I'm new to C++ and I have a question...
I tried answering the question myself by making a test application... in debug, the class B initialization generates less assembly code, but in release mode, I can't really say... it optimizes the initializations away :(
Let's say I have two classes:
class A
{
public:
int a, b, c, d;
A(int _a, int _b, int _c, int _d) : a(_a), b(_b), c(_c), d(_d)
{
}
};
class B
{
public:
int a, b, c, d;
};
Is there any advantage of using
B b = {1, 2, 3, 4}
instead of
A a = A(1, 2, 3, 4);
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于全局对象和静态类成员,初始化列表不会在运行时调用任何代码。 (初始化数据直接存储在二进制文件中)。
如果您正在初始化大量对象,或者构造函数代码昂贵/庞大,这可能会在加载时产生显着差异。
如前所述,这仅适用于普通旧数据,即可以使用 C++ 中的初始化程序列表初始化的所有内容。 0x
For a global objects and static class members, the initializer list doesn't invoke any code on run time. (Initialization data is stored directly in the binary).
If you are initializing a lot of objects, or if the constructor code is expensive / large, this can make a notable difference at load time.
As said, this is true only for plain old data, i.e. everything that can be initialized with an initializer list in C++ < 0x
我不知道性能优势,但通常首选使用构造函数。
这是因为对于 A,成员 a、b、c、d 可以设为私有。 因此,您可以使用 A 方法获得封装,而 B 方法则没有。
作为类设计者,您可以通过构造函数强制严格使用和分配成员变量。 在你的 B 级场景中,你不能。
因此,虽然你可能会在使用 B 时获得性能上的小幅提升,但我敢打赌它可以忽略不计,并且会因拥有不受保护的类成员的潜在头痛而被否定。
I don't know about performance advantages, but in general using the constructor is preferred.
This is because with A, members a,b,c,d can be made private. Thus, you get encapsulation with your A approach, which you don't have in B.
As a class designer, you can enforce strict usage and assignment of member variables via a constructor. In your B, class scenario, you can't.
So while you may get a small boost in perf, for using B, I would wager it to be negligible, and would be negated by the potential headache of having unprotected class members.
您不必在 C++ 中使用这种类型的初始化工作显式编写构造函数和 C 代码。
如果您有简单数据字段的复杂复合结构,则使用初始化列表比使用构造函数初始化变量更容易:
缺点是它(当前)仅适用于仅具有 POD 成员变量的简单类,并且程序员可能不太熟悉语法。
You don't have to explicitly write the constructor and C code using this type of initialization works in C++.
If you have complicated compound structures of simple data fileds, initialization of variables can be easier with initialization lists than with constructors:
The disadvantage that it (currently) only works for simple classes with only POD member variables and that programmers might not be very familiar with the syntax.
大多数类还不能使用初始化列表(还!),因此最好使用构造函数来保持一致。 在 c++0x 中,std::initializer_list 将存在以允许任何类使用此语法。
Most classes can't use initialization lists (yet!), so you are better off using a constructor just to be consistent. In c++0x std::initializer_list will exist to allow this syntax for any class.
以下是使用初始化器列表的场景:
Below are the scenarios when initializer list is used: