DEV Community

Cover image for C++ Programming:  Implicit and Explicit Constructors
Ashish Bailkeri
Ashish Bailkeri

Posted on

C++ Programming: Implicit and Explicit Constructors

Note: Any code snippets posted here are licensed under the MIT License.

Did you know that there are both implicit and explicit constructors?
Let's look into the difference between both, and what each can do for code readability and convivence when programming.

Explicit Constructors

You may see warnings in certain C++ compilers about making certain constructors explicit. But what does it mean? Let's look at an example of an explicit constructor:

class MyClass {
  int i;
  explicit MyClass(int i) : i(i) {}
}

// ...

int main() {
  MyClass clz = MyClass(2);
}
Enter fullscreen mode Exit fullscreen mode

Looks pretty normal, right? So you might wonder what this actually does. You must initialize the value with the name of the type before it's initialization aka: MyClass then with the constructor parameters. You can see the difference in usability in the implicit constructor code.

Implicit Constructors

These constructors allow you to initialize a class value without specifying the name of the class. Let's look at an example:

class MyClass {
  int i;
  /*implicit*/ MyClass(int i) : i(i) {}
}

// ...

int main() {
  MyClass clz = 2;
}
Enter fullscreen mode Exit fullscreen mode

Wait, is the class MyClass now the integer type? No. What actually happened was that the C++ compiler was able to tell that you were calling the MyClass constructor implicitly and allowed that conversion.

Every wondered why this code was legal?

std::string my_string = "Wow, this is cool!";
Enter fullscreen mode Exit fullscreen mode

It's because there is an implicit constructor with std::string that takes in a const char * and thus allows this initialization to be valid.

Bringing It Together

In accordance with certain code writing standards, compilers or code analyzers may warn using implicit constructors, why? They do this because in general it is harder for another programmer to analyze code that uses implicit constructors because it is hard to pin point the type of the object being initialized.

Conclusion

Understanding implicit and explicit constructors will allow you to take in full control of how your code is read and how you use it. This is especially important to take note of if you are reviewing code, or reading code from a library. I hope you learned something today, and have good day!

Discussion (8)

Collapse
aminmansuri profile image
hidden_dude • Edited on

Yes C++ has always allowed implicit "casting" of values. If a suitable constructor has been found.

That is both a blessing and a curse.

Of course it's up to you to design in what your class should or should not have.

Collapse
julienbdubois profile image
JBD

It is more a curse than a blessing to me, there are really few relevant cases where an implicit case can be considered, most of the time, it’s not. std::string::string(char const*) is typically a case where the implicit constructor hide a whole copy of the string and a potential heap allocation.
Implicit things should be trivial, non-trivial things should be explicit.

Collapse
aminmansuri profile image
hidden_dude

Yeah.. C++ can be like that.

Effective C++ covers all these odd cases. The book could also have been called "50 ways C++ can have hidden memory leaks".

Collapse
grave18 profile image
Grave

Yes, and most static analyzers advise you to add explicit keyword to constructors

Collapse
hiteshx123 profile image
Hitesh Ale

Very good.

Collapse
aboss123 profile image
Ashish Bailkeri Author

Thank you! If you have in questions feel free to let me know.

Collapse
guracollen profile image
Collen Gura • Edited on

Good article Ashishi! We need more articles like these.
Just for my education, how did you manage to embed the programming code into your HTML page? I am curious.

Collapse
aboss123 profile image
Ashish Bailkeri Author

The formatting of DEV articles uses the markdown text format and one of the options is to show code inline with its formatting.