Explicit Constructors

Should C++ constructors always be explicit?
deatcAsked:
Who is Participating?
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.

aboo_sCommented:
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)
0

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:
Hi

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.
0
peprCommented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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.

http://en.cppreference.com/w/cpp/language/converting_constructor

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.
1
peprCommented:
@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)?
0
peprCommented:
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...)
0
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
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.