answersLogoWhite

0


Best Answer

Initialization lists makes a difference when we have objects as members. Instead of using default initialization followed by assignment, the initialization list can initialize the object to its final value. This can actually be noticeably faster.

When you need to initialize constant member, references and pass parameters to base class constructors, initialization list is the only choice. if you have members of class type with no default constructor available, initialization is the only way to construct your class.

There is only one way to initialize base class instances and non-static member variables and that is using the initializer list.

If you don't specify a base or non-static member variable in your constructor's initializer list then that member or base will either be default-initialized (if the member/base is a non-POD class type or array of non-POD class types) or left uninitialized otherwise.

Once the constructor body is entered, all bases or members will have been initialized or left uninitialized (i.e. they will have an indeterminate value). There is no opportunity in the constructor body to influence how they should be initialized.

You may be able to assign new values to members in the constructor body but it is not possible to assign to const members or members of class type which have been made non-assignable and it is not possible to rebind references.

For built in types and some user-defined types, assigning in the constructor body may have exactly the same effect as initializing with the same value in the initializer list.

If you fail to name a member or base in an initializer list and that entity is a reference, has class type with no accessible user-declared default constructor, is const qualified and has POD type or is a POD class type or array of POD class type containing a const qualified member (directly or indirectly) then the program is ill-formed.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

An initialisation list is a segment of code that appears between a constructor's signature and its body. It is used to initialise member variables. The segment begins with a colon, followed by a comma-separated list of the member variables and their initial values in parenthesis.

Examples:

class CMyClass

{

public:

CMyClass():

myInt( 0 ),myChar( '0' ) // initialisation list

{}

CMyClass(int, i,char c):

myInt( i ),myChar( c ) // initialisation list

{}

CMyClass(const CMyClass & myClass):

myInt( myClass.myInt ),myChar( myClass.myChar ) // initialisation list

{}

private:

int myInt;

char myChar;

};

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Class initialisation lists are preceded by a colon in C++.

Initialisation lists can only be declared as part of a constructor. Although explicit initialisation is optional, it is good practice to explicitly define your own to ensure initialisation is as efficient as possible. Initialising or assigning members via the body of a constructor creates a two-stage initialisation, because an initialisation list is implied when not explicitly specified.

To declare an initialisation list, place a colon immediately after the constructor name, followed by a comma-separated list of explicit base class constructors and/or member assignments. Base classes and member data should be listed in the same order they were declared by the class. The constructor body immediately follows the initialisation list, but will generally be empty unless further initialisation is required that cannot be handled more efficiently by the initialisation list.

For example:

struct A

{

int m_data;

A(const int data=0): m_data(data) {}

A(const A& rhs): m_data(rhs.m_data) {}

};

When separating the implementation from the interface, the class would look like this:

struct A

{

int m_data;

A(const int data=0);

A(const A& rhs);

};

A::A(const int data): m_data(data) {}

A::A(const A& rhs): m_data(rhs.m_data) {}

When working with base classes, bear in mind that derived class copy constructors must invoke base class copy constructors to ensure efficient construction, like so:

struct base

{

protected:

base(const int data): m_data(data) {}

base(const base& rhs): m_data(rhs.m_data) {}

private:

int m_data;

};

struct derived : public base

{

derived(const int data): base(data) {}

derived(const derived& rhs): base(rhs) {}

};

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When is it necessary to use member wise initialization list in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the order of initialization of data in C plus plus?

The general order of initialization is:Base class objects (if present)Member data objectsConstructor function code


Initialization of variables in namespace in C plus plus?

Is an important thing to do.


What is message passing in c plus plus?

-define class with necessary data member & member function. -create object of that class. -communication.


What is the difference between instantiation and initialization in C plus plus?

Instantiation is creating the instance of the variable/object . While Initialization is to provide the variable with some value. int i; // i is an instance of an integer i=10; //initialised with the value 10


What do you mean by initialization in c plus plus?

Not initialized variable: int myInt; Initialized variable: int myInt = 10;


How do you avoid garbage values in c plus plus programs?

Initialization. Hint: your compiler might warn you, neverignore warnings if you aren't absolutely sure what they mean.


What do you mean by initialisation of objects in c plus plus?

Initialization of objects means to provide an initial value for the object. This is usually done by the constructor, or it can be done with an assignment statement.


Is a paladin a member?

yes paladin is member, sadly plus you need rank10 warroior and healer and its member :(


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


What is the difference between an attribute and a behavior in c plus plus?

An attribute is a class member variable while a behaviour is a class member method.


Is it necessary to know c and c plus plus to learn java?

Of course not.


How this pointer works in C plus plus?

The "this" pointer is a pointer to the instance of the object, with scope within a member function of that object. It is not always necessary to use it, as references to variables defined in the object will be implicitly prefixed with "this->", but it can resolve name scoping problems, and it can make the code more readable.