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.

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may