4

So reading through one of my text books, I came across the initializer for constructors. I would like to know if there is any performance differences and which is the preferred method for general coding standards for C++.

Assume:

  • That the default constructor doesn't take any parameters (so no validtion needed)
  • The class has two private variables: int a and int b
  • Both variables need to be set to 0

Example:

//Setting each private variable with assignment statements
SampleClass::SampleClass() 
{
    a = 0;
    b = 0;
}

//Using initializers to set the private variables
SampleClass::SampleClass() : a(0), b(0) 
{
}



I would imagine if there is a constructor that has parameters and the parameters need some sort of validations, it's best to use the methods that validate within. That being said, would you mix them?

Example:

SampleClass2::SampleClass2(int fNumber, int sNumber) : a(fNumber)
{
    makeSureNotZero(sNumber);
}



Just to clarify the questions:

  1. Of the two methods of setting the variables, which is better for performance?
  2. Is one method generally preferred for general coding standards in C++?
  3. Can you mix both methods?
kingcobra1986
  • 971
  • 2
  • 14
  • 38
  • Please let me know why you put a down vote if you do. I feel that this is a question that is not based on opinion as I am looking for rules and performance. – kingcobra1986 Jul 20 '16 at 01:38
  • 1
    If I am not mistaken main difference is time when initialization occurs. When you use initialization list class members will be initialize according to the order in class declaration and then constrictor will be executed. In second case everything will be initialize inside constructor method. – Logman Jul 20 '16 at 01:58

2 Answers2

3

There is no law against mixing both methods.

In the case of PODs, there is not going to be any measurable difference between either method, and the C++ compiler is likely to generate identical code for both methods of initialization.

On the other hand, if the class members are more complex classes, rather than PODs like ints, constructor initialization will produce better results. This is only a rule of thumb, and the actual results will vary, depending on each particular class. It is not entirely implausible that default-constructing an instance of a class, and then invoking its assignment operator, will give better results than directly constructing it.

Community
  • 1
  • 1
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • For the general C++ coding standards which is preferred? – kingcobra1986 Jul 20 '16 at 01:53
  • 3
    The fascinating thing about C++ coding standards is that everyone has their own coding standard, that's superior to all the rest. – Sam Varshavchik Jul 20 '16 at 01:59
  • @Sam: but mine is **really** superior. – Rudy Velthuis Jul 20 '16 at 02:02
  • 1
    @kingcobra1986 to some point in my life I almost never used initialization lists, then I encountered problem that I could resolve using initialization lists and from that point I prefer lists over assignment in constructor. My advise is just familiar with them, they are really nice to use when you get to know them. – Logman Jul 20 '16 at 02:10
  • *It is not entirely implausible that default-constructing an instance of a class, and then invoking its assignment operator, will give better results than directly constructing it.* That would be a very poorly implemented class IMHO. A member initialization list should be as fast or faster in every case. – NathanOliver Jul 23 '16 at 04:11
  • You may also want to point out that some member must be initialized in the member initialization list like `const` members and references. – NathanOliver Jul 23 '16 at 04:12
1

My answer is it depends on the compiler. Most compilers should be smart enough to optimize both of those, and make them equivalent. If the compiler wasn't a factor then just by looking, and thinking about it I would assume the initializer list. It initializes the member variable with it, instead of having to initialize it and then set it. It might be just a few instructions less, but I would let the compiler handle it.

Taztingo
  • 1,915
  • 2
  • 27
  • 42