Home / C/C++ / When to use “explicit” keyword

When to use “explicit” keyword

  • Use it when the constructor has one and only one argument.

» It has meaning only when applied to the constructor which can be called with exactly 1 argument provided. (Either it has 1 argument or has default values for all other)

  • Use it only on the default, copy, and move constructor.


» It is actually useless for them: default constructor does not have arguments, and move and copy constructors parameters already have the type of that class, so no implicit conversion could happen even in theory.


The gold rule is to use explicit keyword for all constructors which can be called with one argument provided unless you want to enable implicit conversion from the argument type

EXAMPLE:

Download Project (VS2015):

Google Drive 1 Google Drive 2

__FreeSourceC.com__

About admin

Check Also

[ SC Tutorial ] 3. SystemC Data types

SystemC has a number of predefined data types to support hardware designs spanning from the …

4 comments

  1. Hello there, just became aware of your blog through Google, and found that it’s really informative. I抦 gonna watch out for brussels. I抣l appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  2. Hi there would you mind sharing which blog platform you’re
    using? I’m planning to start my own blog soon but I’m having a
    difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m looking for something
    unique. P.S Apologies for being off-topic
    but I had to ask!

Leave a Reply

Your email address will not be published. Required fields are marked *