explicit keyword and explicit constructors in C++

For a constructor there is a default type conversion which happens to the argument to the type of class. However, if you don’t want the explicit conversion to take place then you can use the keyword explicit to avoid the implicit type conversion. The explicit keywords is only good for constructors and can’t be used with any other type.

For Example:

#include <iostream>

using namespace std;

class test {

int i;


explicit test(int j) { i = j; }

int geti() { return i; }


Leave a Reply