Explicit Keyword in C++: A Quick Guide
The explicit
keyword in C++ is used to prevent the compiler from making implicit conversions when calling a function. It ensures that the type of a parameter passed to a function matches the type expected by the function.
Understanding Implicit Conversion
When a function is called, the compiler is allowed to make one implicit conversion to resolve the parameters to a function. This means that the compiler can use constructors that are callable with a single parameter, to convert from one type to another in order to get the right type for a parameter.
An Example
Let’s look at an example class with a constructor that can be used for implicit conversions:
class Foo { private: int m_foo; public: // single parameter constructor, can be used as an implicit conversion Foo (int foo) : m_foo (foo) {} int GetFoo () { return m_foo; } };
Here’s a simple function that takes a Foo
object:
void DoBar (Foo foo) { int i = foo.GetFoo (); }
and here’s where the DoBar
function is called:
int main () { DoBar (42); }
The argument is not a Foo
object, but an int
. However, there exists a constructor for Foo
that takes an int
, so this constructor can be used to convert the parameter to the correct type.
Using the explicit
Keyword
The compiler is allowed to do this once for each parameter. To prevent this, we can prefix the explicit
keyword to the constructor. This will create a compiler error at the function call DoBar (42)
. It is now necessary to call for conversion explicitly with DoBar (Foo (42))
.
The reason we might want to do this is to avoid accidental construction that can hide bugs. For example, you may have a MyString
class with a constructor that constructs a string of the given size and a function print(const MyString&)
(as well as an overload print (char *string)
). If we don’t use the explicit
keyword, then the compiler might implicitly convert char *
to MyString
and call print(const MyString&)
instead of print (char *string)
, which can produce unexpected results.
To Summarize
The explicit
keyword in C++ is used to prevent the compiler from making implicit conversions when calling a function. It ensures that the type of a parameter passed to a function matches the type expected by the function. By using the explicit
keyword, we can avoid accidental constructions that can hide bugs.
FAQs
What is the purpose of the explicit
keyword?
The purpose of the explicit
keyword is to prevent the compiler from making implicit conversions when calling a function. It ensures that the type of a parameter passed to a function matches the type expected by the function.
When should the explicit
keyword be used?
The explicit
keyword should be used when you want to avoid accidental constructions that can hide bugs. For example, if you have a MyString
class with a constructor that constructs a string of the given size, you can use the explicit
keyword to prevent the compiler from implicitly converting char *
to MyString
when calling a function.
Leave a comment