Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the explicit keyword mean?

What does the explicit keyword mean in C++?

like image 974
Skizz Avatar asked Sep 23 '08 13:09

Skizz


People also ask

What is explicit keyword in Java?

The explicit -keyword can be used to enforce a constructor to be called explicitly. class C { public: explicit C() =default; }; int main() { C c; return 0; } the explicit -keyword in front of the constructor C() tells the compiler that only explicit call to this constructor is allowed.

What is explicit QT?

Explicit means, in this case, that a QWidget* cannot be implicitly converted to a MainWindow object. The :QMainWindow(parent) simply says that the base class constructor which takes a QWidget* as parameter should be called to construct the object.

What is this keyword C++?

In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used to pass current object as a parameter to another method. It can be used to refer current class instance variable. It can be used to declare indexers.

What is implicit call in C++?

A default constructor is one which takes no parameters, an implicit call is when you directly call the constructor.


1 Answers

The compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use constructors callable with a single parameter to convert from one type to another in order to get the right type for a parameter.

Here's 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.

The compiler is allowed to do this once for each parameter.

Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor for implicit conversions. Adding it to the above class 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 you might want to do this is to avoid accidental construction that can hide bugs.
Contrived example:

  • You have a MyString class with a constructor that constructs a string of the given size. You have a function print(const MyString&) (as well as an overload print (char *string)), and you call print(3) (when you actually intended to call print("3")). You expect it to print "3", but it prints an empty string of length 3 instead.
like image 88
22 revs, 17 users 57% Avatar answered Oct 02 '22 00:10

22 revs, 17 users 57%