# explicit constructors

This topic is 2302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

As I was coding recently I stumbled upon the subject of explicit constructors. I was surprised to learn that you could easily fool your compiler into constructing class objects out of single parameters like this:

 MyClass myClass = 123; 

This can make your code tricky to understand especially if you are writing functions that accept "MyClass" objects but you instead pass in a value. Another example where this can happen is if you are using the comparison operator.

I decided to create a video tutorial on the subject (http://www.marek-kno...hp5?vmk=cpp21p1) describing when you should and shouldn't be using the explicit keyword. From now on, I'm going to be using the explicit keyword!

##### Share on other sites
So you've only just stumbled upon them and you've already created a video tutorial on how to and how not to use them?

##### Share on other sites
Marek,

Explicit constructors have been in placed for years. It's standard c++. You should always use explicit (whereever possible, whenever possible).

What to bear in mind is that this form, you wrote, does call a constructor and not the assignment operator. It doesn't fool the compiler, unless your compiler is a very old one (which probably would fail).
Without explicit specified, your compiler will call your constructor with a matching parameter.

It's programmer-fool, rather than compiler-fool. In other words, "Marek-didn't-know.com"

:-)

##### Share on other sites
They do come in handy for cases such as:
 std::string s = "Hello, World". 
Like overloaded operators, it should be restricted to cases where it makes sense, which is typically for value types with universally understood semantics.

Unfortunately, it is one of those annoying C++ features that is on by default, and you have to go out of your way to disable it. Personally, I don't bother with this one. It will bite me eventually, and then I might change my tune.

##### Share on other sites
It's one of those topics that was never taught in any of the programming classes that I took!

Unless it is something you know to look out for, it is very easy to never see the compiler automatically construct class objects for you on your behalf.

##### Share on other sites

It's one of those topics that was never taught in any of the programming classes that I took!

There should be a "driver's license" for anyone attempting to do anything in C++. It should require understanding the absolute basics.

Unless it is something you know to look out for, it is very easy to never see the compiler automatically construct class objects for you on your behalf.[/quote]

Any variable declared is automatically constructed by compiler. From char, int, X * (nop constructors) to classes (constructor may or may not be called). Passing parameters by value also does the same.

It may not be obvious, but is a crucial feature of C++ to understand and probably one of its more distinctive features, since it's the principle behind RAII.