I read that it's a good practice to define single argument constructors explicit
in order to avoid implicit conversions. I understand the pitfall of having int value promoted to class object. I wonder if it also applies to the constructors accepting reference types. How one can provoke implicit conversion in this case:
class Foo
{
public:
Foo(Bar& bar) { }
};
Does the situation changes if the constructor accepts pointers, is conversion from NULL
and nullptr
possible ?
class Foo
{
public:
Foo(Bar* bar) { }
};
Yes to both. A function with signature
will make the compiler to create a
Foo
if you pass aBar
there.Same for
0
andnullptr
.