Answer to Question #180574 in C++ for Gaurav Kumar

Question #180574

Explain the difference between 

constructor and initializer list. [


1
Expert's answer
2021-04-13T03:51:39-0400

Constructors are, first and foremost, just functions, and so they can be very simple, very complex, or anything in between. However, constructors are special functions that are called automatically whenever a new object is created (i.e., instantiated). The primary purpose of constructors is to construct or initialize an object. Visually, constructors are set apart from "regular" functions by two characteristics: (a) they have the same name as the class for which they are constructing objects, and (b) they do not have a return type.



Initializer List is used in initializing the data members of a class. The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon.Initializer lists are used with constructors to initialize (i.e., to assign the first or initial value to) an object's member variables. Prior to the ANSI 2014 standard, initializer lists were the only way to initialize member variables, and they remain the preferred way to initialize data members with values passed in to the constructor as arguments.




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS