Tuesday, February 28, 2012
What is initialization list in C++?
Generally in C++, we will initalise the variables in constructors using assignment operator. But there are some scenarios like const and reference variables, where we need to initialise at the time of declaration. Assignement operator will not work for these. C++ provides new feature initialisation list to do this. The initialisation list is required in inheritance and compostion for initialising data members.
The Initialisation list should add after the constructor, starts with a colon (:) and each variable with the value enclosed in brackets and separated by comma. It will not end with semicolon(;). If you use initialisatin list, no need off explicit assignment of the variables in constructor. See the sample code below.
the above two constructors are same as below.
The Initialisation list should add after the constructor, starts with a colon (:) and each variable with the value enclosed in brackets and separated by comma. It will not end with semicolon(;). If you use initialisatin list, no need off explicit assignment of the variables in constructor. See the sample code below.
class inList{ private: int x,y; public: inList():x(10),y(20) //initialisation list { //nothing } inList(int aX, int aY):x(aX),y(aY) // initialization list { //nothing } }; int main() { inList l; inList iL(100,200); }
the above two constructors are same as below.
inList() { x=10; y=20; } inList(int aX, int aY) { x=aX; y=aY; }
Example for Inheritance:
class inList{ private: public: int x,y; inList():x(10),y(20) { //nothing } inList(int aX, int aY):x(aX),y(aY) { //nothing } }; class inListDerive: public inList { private: int z; public: inListDerive():z(10),inList(20,30) { //nothing } inListDerive(int aZ):z(aZ),inList(20,30) { //nothing } }; int main() { inListDerive dL; inListDerive dL1(100); }
P.S: To see initialising const variable using initialisation list click here.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
No comments:
Post a Comment