Testing has a non explicit constructor that accepts an int. Therefore, int can be implicitly converted to Testing by creating a temporary object.
Since Show accepts a const Testing & (and not just a Testing & ), you can pass a temporary one to it. Finally, the second parameter is optional, so you do not need to specify a value for this.
This whole mechanism allows you to do this, by the way:
void f(const std::string &str);
Here "Hello" is of type const char (&)[6] , which splits into a const char * , but you can build std::string from const char * , which allows you to use a const char * , where a std::string .
Keep in mind that this creates a temporary and, therefore, valid only for parameters that are passed by value or by reference const (for links will not). In addition, the constructor should not be marked explicit .
Etienne de martel
source share