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

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



Posted Date: 3/15/2013 6:00:45 AM | Location : United States

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


Posted by | Posted Date: 3/15/2013 6:01:10 AM

Related Discussions:- Should my constructors employ"assignment"or"initialization, Assignment Help, Ask Question on Should my constructors employ"assignment"or"initialization, Get Answer, Expert's Help, Should my constructors employ"assignment"or"initialization Discussions

Write discussion on Should my constructors employ"assignment"or"initialization
Your posts are moderated
Related Questions
Write a program that counts from 0 to 99 and prints these numbers to the terminal ie it prints 00, 01, 02, 03, 04, 05, ....,99. Graphics 1. Write a program in rPeANUt that

A: Use p = new T[n] and delete[] p:   Fred* p = new Fred[100]; ... delete[] p; Any time you allocate an array of objects through new (generally with the [n] in the n

I really have a hard time analyzing how to write a code in C. It''s all bout Time Departure and we must used a 24 hour clock. Can u help me?

Available in the string.h header file are the following routines         strcat(string1,string2);   This joins string2 to string1; care must be taken to allocate enough space for

Normal 0 false false false EN-US X-NONE X-NONE

Binomial coefficients are the numeric factors of the products in a power of a binomial such as (x + y)n. For example, (x + y)2 = x2 + 2 x y + y2 has the coefficients 1 2 1. Binomia

Problem: (a) Using a class hierarchy of your own choosing, with at least TWO subclass levels, show (i) the use of abstract and concrete forms of Java class and method in you

Define some features of static storage class in c program? The features of a variable defined to contain a static storage class are as follows. Storage - memory Default

flowchart of c programing to check the given number is prime or not