Bar bar = static_cast<Bar>(foo);
This cast will fail. Foo and Bar are incompatible types, if only at least one of the following statements:
Foo sourced from Bar , OrBar has a constructor that accepts Foo , OrFoo has a custom conversion to Bar .
The big question here is not whether it will be issued successfully or not. The bigger and more relevant question should be: what do you want to get out of such an actor? Why do you do this in the first place? What should he do? I mean, how would a Bar object be initialized from a Foo object?
A reasonable way to convert one type to another is one of the following methods:
Define Foo as:
class Foo : public Bar {
Or define Bar as:
class Bar { public: Bar(const Foo &foo);
Or provide a conversion function in Foo like:
class Foo { public: operator Bar();
Nawaz source share