I have a template class called Variable, with a specialized constructor for char*, which is defined as follows:
template<>
Variable<char*>::Variable(char * const &arg_value)
{
value = new char[strlen(arg_value) + 1];
strncpy(value, arg_value, strlen(arg_value));
value[strlen(arg_value)] = '\0';
}
Now, I have this statement, that declares a Variable<char*>:
Variable<char*> stringVar = const_cast<char*>("Hi");
In my Variable definition, I never declared or defined a copy constructor to a const char*. However, the statement works perfectly fine. Why is this? I am quite positive that stringVar has a data-type of Variable<char*>, but this still works. Where is this assignment coming from?
A constructor that takes one argument allows for implicit conversions. Here’s a simpler example of your situation:
To inhibit this implicit conversion, say
explicit: