You are here:

C++/parameterised constructor


Yesturday I was reading about parameterised constructors. I read "There are two types of calling methods one is implicit and other is explicit." I know the basic difference of syntax between these two. I just want to know is whether there is any difference between working of these two? How can we identify where to use implicit and where to use explicit? I think there should be something different in there working as we need to define it diffeterntly. Can you specify how they works? and which one to use where and why?
Thanks in advance.

Hello Kedar

Implicit construction can occur when a constructor needs only one parameter. It might take only one parameter or it might take many but with defaults specified for all parameters after the first one. Only those types of constructors are affected by the explicit keyword.

Having explicit construction prevents accidental or unexpected object creation. This is especially important if the object creation takes a lot of time, memory, or other resources.

In my opinion, all such constructors should start out as explicit. If there is a good reason to remove the explicit keyword, it can be removed and replaced with a comment explaining why implicit construction is needed.

Making constructors explicit increases restrictions on them in the same way that making members private, and making parameters const increases restrictions. Increasing restrictions leads to fewer surprises because the programmer is prevented from accidentally doing things which should not be done.

Here are links for two good explanations of explicit constructors. I don't think I need to repeat their words here.

Best regards


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2016 All rights reserved.