Is it possible and why one would want to do it?
class Foo;
class Bar;
......
Foo foo;
Bar bar = static_cast<Bar>(foo);
Normally static_cast is used with numeric types and pointers, but can it work with user defined data types, a.k.a classes?
Is it possible and why one would want to do it?
class Foo;
class Bar;
......
Foo foo;
Bar bar = static_cast<Bar>(foo);
Normally static_cast is used with numeric types and pointers, but can it work with user defined data types, a.k.a classes?
Bar bar = static_cast<Bar>(foo);
This cast will fail. Foo
and Bar
are incompatible types, unless atleast one of the following is true:
Foo
is derived from Bar
, OrBar
has a constructor that takes Foo
, OrFoo
has a user-defined conversion to Bar
. The bigger question here is not whether it will cast successfully or not. The bigger and the actual question should be: what do you want to get out of such cast? Why would you want to do such a thing in the first place? What is it supposed to do? I mean, how would the Bar
object be initialized from Foo
object?
The sensible way to convert one type to another is one of the following ways:
Either define Foo
as:
class Foo : public Bar
{
//...
};
Or define Bar
as:
class Bar
{
public:
Bar(const Foo &foo); //define this constructor in Bar!
};
Or provide a conversion function in Foo
as:
class Foo
{
public:
operator Bar(); //provide a conversion function Foo to Bar!
};
Here's the rule:
The expression static_cast<T>(e)
is valid if and only if the following variable definition is valid
T x(e);
where x
is some invented variable.
So, in general, two unrelated types cannot be cast to one another. However, if one type is derived from the other, or when one defines a conversion function to the other, or has a constructor taking a single argument of the other type - in these cases static_cast will be well-defined.
Does it ever make sense? For example, if T defines a constructor that takes a single U and the constructor is explicit
then static_cast<T>(e)
where e is of type U would make perfect sense