is ``: m_var(var)`` faster than ``m_var = var`` [duplicate]

There are two ways of quickly setting up a class that I know of

A:

MyClass::MyClass(File& file) :
    m_file(file)
{}

B:

MyClass::MyClass(File& file) :
{
    m_file = file;
}

is there a performace related reason to use one over the other or is it just coding standards.


Solution 1:

It depends.

In the first case, m_file is constructed directly from file.

In the second case, m_file is default-constructed first, then updated from file, undoing whatever the constructor had initialized.

So, depending on what type m_file is declared as, and what its default constructor does or does not do, there may be performance differences.