In C++, we could restrict the conversion of constructor’s argument to use direct-initialisation instead of copy-initialisation.
The differences between them are shown below:
Vector v1{7}; // direct-initialisation
Vector v1 = 7; // copy-initialisation
To do this, we could add the specifier explicit
onto the Vector
constructor:
class Vector {
public:
explicit Vector(int s);
}
The compiler will recognise that restriction on the conversion is necessary on the Vector
and prohibit such conversion during compile time.
Vector v1{7}; // OK
Vector v1 = 7; // error in compile time