GeekInterview.com
Series: Subject: Topic:
Question: 17 of 267

Constructor initialization lists

What is advantage of using Constructor initialization lists?
Asked by: ak.nextptr | Member Since Jan-2012 | Asked on: Jan 17th, 2012

View all questions by ak.nextptr   View all answers by ak.nextptr

Showing Answers 1 - 3 of 3 Answers
Rudresh

Answered On : 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

  
Login to rate this answer.
abc

Answered On : 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)

  
Login to rate this answer.
Paul_Singh

Answered On : Feb 7th, 2012

View all answers by Paul_Singh

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.