Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to make constructor explicit

Tags:

c++

When designing a public API, is it a good practice to make the constructor as explicit?

class A {
public:
    //explicit A(int i){}
    A(int i){}
};

void fun(const A& a) {}

int main() {
    // If I use explicit for A constructor, I can prevent this mistake.
    // (Or shall I call it as feature?)
    fun(10);
}

Or shall I allow implicit conversion, to allow user to call my API with less typing?

like image 982
Cheok Yan Cheng Avatar asked Sep 15 '10 09:09

Cheok Yan Cheng


People also ask

Can constructor be called implicitly?

The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method. Example e = Example(0, 50); // Explicit call. Example e2(0, 50); // Implicit call.

What is explicit constructor Java?

Explicit means done by the programmer. Implicit means done by the JVM or the tool , not the Programmer. For Example: Java will provide us default constructor implicitly. Even if the programmer didn't write code for constructor, he can call default constructor.

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.

Which of the following are true about conversion constructor in C++?

Conversion constructor in C++? It has some unique property like, its name will be same as class name, it will not return any value etc. The constructors are used to construct objects of a class. Sometimes constructors may take some arguments, or sometimes it does not take arguments.


3 Answers

The constructor should be explicit, unless an implicit conversion makes sense semantically (e.g. what is the meaning of converting an int to an A?). Less typing should not be the criterion to guide that decision. Think about readability (which is the main argument for implicit casting) and how well your code is to understand. An implicit cast that is not intuitive will make readers of the code scratch their heads.

P.S.: I cannot seem to come up with a good example right now, so any help is appreciated.

like image 71
Björn Pollex Avatar answered Nov 09 '22 20:11

Björn Pollex


Yes, by default any constructor, which can be called with one argument should be explicit. Following this rule will avoid subtle bugs, which are extremely hard to find.

Of course, there are exceptions to this rule:

  • Implicit conversion might be desireable, if your class has the semantics of a wrapper around the one parameter's type.

  • Copy constructors should not be explicit (otherwise you loose the possibility for pass-by-value calls).

like image 25
Ralph Avatar answered Nov 09 '22 19:11

Ralph


This is what I found in a resonse from "Daniel Krügler"

If we would have started to design C++ from today on, there is a good chance, that all constructors and conversion functions were "explicit" by default, but a user could make the "implicit". Alas, the time cannot be turned back and we have to live with the current state. This means that we have to be careful in regard to implicit constructors (except for the copy/move constructor). It is a safer rule to make constructors explicit, where some form of conversion is involved (i.e. any constructor with an argument type U different from the actual type T).

like image 37
Chubsdad Avatar answered Nov 09 '22 20:11

Chubsdad