Explicit Constructors

Should C++ constructors always be explicit?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can have more than one constructor with different parameters, and even if you declare none one will be auto built, call it the default maybe.. so the answer is no they don't have to  be!
(If I understand you question)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kanti PrasadCommented:

Only constructors with one argument should be explicit as it avoids undesirable conversions which will lead to creation, of new objects without your knowledge.

Copy constructors  should not be explicit and Classes that are intended to be transparent wrappers around other classes are also exceptions.
Also, constructors that take only an initializer_list may be non-explicit.
Simple things should look simple (and be simple inside). Once you know that things are not that simple, you can explicitly suppress the default constructor by declaring it private.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

evilrixSenior Software Engineer (Avast)Commented:
>> Should C++ constructors always be explicit?
No. The explicit keyword should only be used when you need to prevent constructor conversion (casting). Sometimes you want to allow constructor casting. If you don't want your constructor to be automatically used to cast from one type to the type of the class the constructor represents then make it explicit.


A good example of where you do not want to use explicit is the std::string constructor that takes a char const * type. This allows the compiler to automatically convert a c-string to a c++ std::string. If it was explicit the compiler wouldn't do this and so a lot of code wouldn't work without you explicitly wrapping things up in a std::string first.

Before you start using explicit I suggest you create some test code with and without it and explore how the behaviour differs.
@deatc: Do you mean using the explicit keyword as evilrix pointed out, or just explicit definition of otherwise default constructor (from pre C++11 standard)?
It looks like some bug in the system (?) I have found e-mail from 21st July that it was already accepted that day. And if I recall correctly, it really was. I remember it was strangely accepted because of all the same values for each comment. (But I may have overlooked something...)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.