Why initialization list is used
Boolean - Integer - Floating-point. Implicit conversions - Explicit conversions. Class declaration. Access specifiers. Virtual function. Default constructor. Copy constructor. Copy assignment. Class template. Function template.
Template specialization. Copy elision. Static initialization. Dynamic non-local initialization. Ordered dynamic initialization. Go to Log In. Forgot your password. Not registered yet? Go to Sign Up. In the previous chapter, we learned about how classes and their objects can be created and the different ways their members can be accessed.
We also saw how data members are initialized in the constructor of any class as shown below. In the above constructor, we assigned the values 7 and 4 to the data members length and breadth respectively. Note that, here we assigned the values to these variables, not initialized. In the case of constructors having parameters, we can directly initialize our data members using initialization lists. An initializer list starts after the constructor name and its parameters and begins with a colon : followed by the list of variables which are to be initialized separated by a comma with their values in curly brackets.
Here we initialized the variables length and breadth directly using an initializer list in which we length and breadth were initialized to 7 and 4 respectively. The rest of the code is the same. One exception is discussed further down. The most common benefit of doing this is improved performance. Even if the types are not the same, the compiler is usually able to do a better job with initialization lists than with assignments. Then that temporary object is destructed at the ;. That's inefficient.
As if that wasn't bad enough, there's another source of inefficiency when using assignment in a constructor: the member object will get fully constructed by its default constructor, and this might, for example, allocate some default amount of memory or open some default file.
Conclusion: All other things being equal, your code will run faster if you use initialization lists rather than assignment. But even in these cases, my personal preference is to set those data members in the initialization list rather than via assignment for consistency. Now for the exceptions.
Sergey, The A's default constructor would still be called. Show 3 more comments. Naveen Naveen Same goes for const members I believe. Initialization of base class One important reason for using constructor initializer list which is not mentioned in answers here is initialization of base class.
Amal K 3, 2 2 gold badges 13 13 silver badges 36 36 bronze badges. Jamal Zafar Jamal Zafar 2, 2 2 gold badges 27 27 silver badges 32 32 bronze badges. Rahul Singh Rahul Singh 55 4 4 bronze badges. While this code snippet may solve the question, including an explanation out of the code really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! Please, write your own understanding or just share link to original source here, geeksforgeeks. To avoid, we must have either 1. Default constructor of Sample1 class 2. To avoid, we must use either 1. Initialization list 2. Eswaran Pandi Eswaran Pandi 3 3 silver badges 10 10 bronze badges.
Yi Wang Yi Wang 11 1 1 bronze badge. Daniel Daniel 1 1 silver badge 12 12 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.
0コメント