Casting means "changing an entity of one data type into another". That said, you can consider Integer()
as a cast from long
to Integer
, as the two types are related and the operation translates into "build an object of type B, starting with an object of type A".
With this syntax, there is no protection against misuse, i.e. if the constructor takes only one parameter, the parameter might not be used to build an object directly representing the first (e.g. each QWidget
takes a pointer to the parent, but it is not representing its parent, obviously), and you cannot do anything to prevent this. You could block implicit initialization by marking single-parameter constructor as explicit
, but nothing more.
The syntax for old-style casts and constructors with only one parameter is exactly the same, and that's the reason why a new syntax was created for the first: use new style (explicit) C++ syntax for casts, that is, const_cast
, dynamic_cast
, static_cast
or reinterpret_cast
as appropriate.
In the very words of Bjarne Stroustrup, this verbose casting syntax was introduced to make clear when a cast is taking place. Note that having four forms also allows for proper differentiation of the programmer's intent.
Finally, int()
and such are considered old-style for plain types (int
, long
, etc.) and newvar = (T)oldvar
form exists only because of C compatibility constraint.