devxlogo

Class constructor

Class constructor

Question:
I’ve read that there are two ways to define a class constructor. For example:

class List…1. List():num(0)   {     }2. List()   {       num=0;    }
Why is the first one more efficient at runtime?

Answer:
Actually it is more that an issue of efficiency, it is also one of choice or requirement. A class consists of data members and member functions that define the characteristics of an object constructed based on that class. The data members of a class are objects themselves; therefore, when an object of a given class is constructed, the data members of that class are constructed prior to the object.

When an object is being constructed (or instantiated, or created, etc.) its constructor is called. If you do not explicitly reference a particular constructor at the time of instantiation, it will call a default constructor, which is a constructor that takes no parameters.

So in the case of a class, all data member constructors get called prior to class constructor. If you do nothing, the default constructors of those data members will be called. Then, after all your data members have been instatiated, your class constructor will be called prior to the object of that class being created. A constructor is a special member function that allows you to perform initialization of your data members at construction time.

Regarding efficiency in your example, you explicitly initialize the contents of num by placing it in the initialization list area of the List class and explicitly reference a particular constructor passing it the value that you want the internal data members of num to be set at. In the second part of your example, num has already been constructed, but has been initialized with an unknown value; therefore, assign it the value you want. So in effect you have done an operation in two steps that could have been done in one.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email

In your example, we could make the assumption that num is a primitive data type that is one of the default data types provided to you from the C++ compiler. So you will not see much benefit either way. However, if you create your own user-defined type that is a composite, you may definitely witness the difference in construction time of an object, depending on the type of data members a given class contains.

The other reason is one of choice or requirement. In many cases, data members of a class will initialize a default constructor’s internal attribute values to suit your liking; therefore there may be no need for you to explicitly initialize those values to anything else. So doing nothing could be the best thing. In another case, you may choose to do what you’ve illustrated above.

Finally, in yet another case, you may have no choice. For example, if you have a data member that doesn’t have a default constructor, you must explicitly initialize it in the initialization list of areas of the constructor definition as you so illustrated in the first part of your example.

In another situation, if you have a data member that is a reference variable, it must be assigned the address of another object variable upon its inception. This would have to occur in the initialization list area of a class constructor as well.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist