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.

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing