Should my constructors employ"assignment"or"initialization, C/C++ Programming

Assignment Help:

Should my constructors employ "assignment" or "initialization lists"?

 

 


Related Discussions:- Should my constructors employ"assignment"or"initialization

Padovan string, write a javaprogram to solve padovan string using java? ...

write a javaprogram to solve padovan string using java? program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring;

Data types, #question write a prog c & cpp

#question write a prog c & cpp

Explain the polymorphism in object-oriented programming, Problem: (a) ...

Problem: (a) A GUI can contain text-fields, buttons, and other labels. A button usually triggers an event on the GUI. Explain the different processes in registering an event

Multilevelinheritance inc++, develop a program read the following informati...

develop a program read the following information from the keyboard in which base class consist of employee name code and destingnation the derived class contain the data members th

Variables within c, Within software languages we have the ability to store ...

Within software languages we have the ability to store information in mail boxes i.e. memory slots which are given names to represent the box. The naming rules are governed by the

Coding, Problem Statement: You have to write a C program to develop a Car P...

Problem Statement: You have to write a C program to develop a Car Parking Management System for a busy commercial area. The system will record the car plate number, date and time w

Summations, How do you write the code for summations

How do you write the code for summations

ASCII, A string S is said to be "Super ASCII", if it contains the character...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Described friend?, A: Something to let your class to grant access to anothe...

A: Something to let your class to grant access to another class or function. Friends may be either classes or functions. Class grants access privileges to its friends. In genera

Enumeration types, Write a simple program in C++ to investigate the safety ...

Write a simple program in C++ to investigate the safety of its enumeration types. Include at least 10 different operations on enumeration types that are incorrect/unsafe things to

3/15/2013 6:01:10 AM

A: Initialization lists. Actually constructors must initialize as a rule all member objects in the initialization list. One exception is discussed further down.

Suppose the following constructor which initializes member object x_ by using an initialization list: Fred::Fred() : x_(whatever) { }. The most common benefit of doing this is improved performance. For instance, if the expression whatever is the same kind as member variable x_, the result of the expression is directly constructed inside x_ the compiler does not make a separate copy of the object. Though the types are not the same, typically the compiler is able to do a better job with initialization lists than with assignments.

The other (inefficient) way to build constructors is through assignment, like: Fred::Fred() { x_ = whatever; }. In this particular case the expression whatever causes a separate, temporary object to be developed, and this temporary object is passed into the x_ object''s assignment operator. Then that temporary object is destructed at the;. That''s incompetent.

As if that wasn''t bad sufficient, there''s another source of inefficiency while using assignment in a constructor: the member object will get completely constructed by its default constructor, and this might, for instance, allocate some  of the default amount of memory or open some default file. All of this work could be for naught if the whatever expression and/or assignment operator causes the object to shut that file and/or release that memory (for example if the default constructor didn''t allocate a large sufficient pool of memory or if it opened wrong file).

Conclusion: All of other things being equal, your code will run faster if you use initialization lists instead of assignment.

Note: There is no performance difference if the kind of x_ is some built-in/intrinsic type, like int or char* or float. However even in these cases, in according to me preference should be to set those data members in the initialization list instead of via assignment for consistency. Another symmetry argument in favor of by initialization lists even for built-in/intrinsic types: non-static const & non- static reference data members can''t be assigned a value in the constructor, thus for symmetry it makes sense to initialize everything in the initialization list.

For the exceptions now every rule has exceptions and there are a couple of exceptions to the "use initialization lists" rule. Bottom line is to employ common sense: if it''s cheaper, better, faster, etc. to not use them, then by every means, don''t use them. It might happen while your class has two constructors that require initializing the object''s data members in distinct orders. Or it might happen while two data members are self-referential. Or while a data- member require a reference to the this object, and you wished to ignore a compiler warning regarding using the keyword prior to the {that start the constructor''s body (while your specific compiler happens to issue that specific warning). Or while you require to do an if/throw test on a variable ( global, parameter etc.) prior to via that variable to initialize one of your this members. This list is not exhaustive

 

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd