Constructor initialization lists

What is advantage of using Constructor initialization lists?

Questions by ak.nextptr   answers by ak.nextptr

Showing Answers 1 - 9 of 9 Answers

Rudresh

  • Jan 18th, 2012
 

For a global objects and static class members, the initializer list doesnt invoke any code on run time. (Initialization data is stored directly in the binary).

If you are initializing a lot of objects, or if the constructor code is expensive / large, this can make a notable difference at load time.

As said, this is true only for plain old data, i.e. everything that can be initialized with an initializer list in

  Was this answer useful?  Yes

abc

  • Feb 5th, 2012
 

--> initializer list is used with constructor when any const datas are used in a class.
--> bcoz, as we known const var should be initialized while declaring.
--> the class gets memory allocation oly wen an obj is created for it.
--> when an obj is created, compiler internally calls constructor, which will initialize the const using the initiailizer list.
Syntax:
classname (arg1,arg2....):initializer_list
eg:
car (char*name,float price,int reg_id):reg_id(i)

  Was this answer useful?  Yes

Paul_Singh

  • Feb 7th, 2012
 

For some object the initialized list is necessary because they are const or do not have default constructors. However, the main advantage is that if you construct the object in the constructor body then you will be creating a temporary object AND calling the assignment operator as opposed to creating it in the initialiser list where you will just be calling the constructor.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions