Skip to main content
gamedev.net
🔒 Locked

explicit constructors

Started by mmakrzem Nov 4, 2011 at 11:05 AM 4 replies 1.2k views
Original Post
mmakrzem
mmakrzem
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!
wforl
wforl
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?
ddlox
ddlox
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"

:-)
rip-off
rip-off
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.
mmakrzem
mmakrzem
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.
Antheus
Antheus

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.