什么都不做的默认构造函数会影响性能吗?
Can default constructor that does nothing affect performance?
我正在经历 source code of Box2D and got stumbled upon the following code。
/// A 2D column vector.
struct b2Vec2
{
/// Default constructor does nothing (for performance).
b2Vec2() {}
...
}
构造函数没有初始化任何字段或为此进行任何操作。
有无空构造函数如何影响性能?
如果它不存在,那么你只能使用另一个构造函数创建一个对象。该构造函数初始化数据成员,这比不初始化它们要慢。因此,如果您还不需要给它们赋值,您可以出于性能原因使用此构造函数。
我正在经历 source code of Box2D and got stumbled upon the following code。
/// A 2D column vector.
struct b2Vec2
{
/// Default constructor does nothing (for performance).
b2Vec2() {}
...
}
构造函数没有初始化任何字段或为此进行任何操作。
有无空构造函数如何影响性能?
如果它不存在,那么你只能使用另一个构造函数创建一个对象。该构造函数初始化数据成员,这比不初始化它们要慢。因此,如果您还不需要给它们赋值,您可以出于性能原因使用此构造函数。