Tanmay Patil
Tanmay Patil

Reputation: 7057

Can default constructor that does nothing affect performance?

I was going through source code of Box2D and got stumbled upon the following code.

/// A 2D column vector.
struct b2Vec2
{
        /// Default constructor does nothing (for performance).
        b2Vec2() {}

        ...
}

The constructor is not initializing any fields or doing any operation for that matter.

How can having or not having an empty constructor affect performance?

Upvotes: 2

Views: 440

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254461

If it didn't exist, then you could only create an object using the other constructor. That constructor initialises the data members, which is slower than not initialising them. So you can use this constructor for performance reasons if you don't need to give them values yet.

Upvotes: 3

Related Questions