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 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 has the type of that class, so no implicit conversion could happen even in theory.


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

EXAMPLE:

Download Project (VS2015):

Google Drive 1 Google Drive 2

__FreeSourceC.com__

About admin

Check Also

Simulation Engine in SystemC

SystemC is applied to system-level modeling, architectural exploration, performance modeling, software development, functional verification, and …

Leave a Reply

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