When a class constructor in C++ has only one parameter and that parameter isn't of the same type as that class, it is assumed to be a conversion constructor for converting the passed type to the type of the class. For example:

class HelloWorld
{
private:

char * mydatahere;
public:
HelloWorld(int);
}

While the inexperienced programmer may think that the constructor in this function takes an integer variable, infact the compiler expects a function that will convert the integer to a HelloWorld object. Now, if this isn't what you want, you need to use the explicit keyword. This will make sure the compiler treats the constructor as a normal constructor, and not as a conversion constructor. Like so:

class HelloWorld
{
private:

char * mydatahere;
public:
explicit HelloWorld(int);
}

Log in or register to write something here or to contact authors.