In C++, assume following class hierarchy:
class BaseClass { };
class ChildClass : public BaseClass { };
Further assume factory classes for these two classes with a common, templated base class:
template<typename T>
class Factory {
public:
virtual T* create() = 0;
};
class BaseClassFactory : public Factory<BaseClass> {
public:
virtual BaseClass* create() {
return new BaseClass(&m_field);
}
private:
SomeClass m_field;
};
class ChildClassFactory : public Factory<ChildClass> {
public:
virtual ChildClass* create() {
return new ChildClass(&m_field);
}
private:
SomeOtherClass m_field; // Different class than SomeClass
};
Note that the size/internal structure of ChildClassFactory and BaseClassFactory is different due to their different fields.
Now, if a have an instance of ChildClassFactory (or Factory<ChildClass>), can I safely cast it to Factory<BaseClass> (via reinterpret_cast)?
Factory<ChildClass>* childFactory = new ChildClassFactory();
// static_cast doesn't work - need to use reinterpret_cast
Factory<BaseClass>* baseFactory = reinterpret_cast<Factory<BaseClass>*>(childFactory);
// Does this work correctly? (i.e. is "cls" of type "ChildClass"?)
BaseClass* cls = baseFactory->create();
I know that you can’t always cast templated classes this way, but in this special case a cast should be safe, shouldn’t it?
I’ve tested it with Visual C++ 2010 and it does work. My question now is whether this is portable to other compilers?
Update: Since there has been some confusion let me clarify some more what’s (supposed to be) important in my example:
ChildClassis a child class ofBaseClass- A user of
Factory<BaseClass>doesn’t know what child class ofBaseClasswill be created. All he knows is thatBaseClassis created. Factory<T>has no fields of its own (other than the vtable).Factory::create()isvirtual
No, it is not. You may not use the result of a
reinterpret_castother than to cast stuff back, except for a few special cases:ISO14882:2011(e) 5.2.10-7:
To make a possible failure scenario more clear, consider multiple inheritance, where using a
static_castordynamic_castwould sometimes adjust the pointer value, butreinterpret_castwill not. Consider casting in this example fromA*toB*:To understand how your code fails in a different way too, consider how most compilers implement the virtual function call mechanism: With virtual pointers. Your instance of
ChildClassFactorywill have a virtual pointer, pointing to the virtual table ofChildClassFactory. Now when youreinterpret_castthis beast, it just happens to “work” incidentally, because the compiler expects some virtual pointer, pointing to a virtual table that will have the same/similar layout. But it will still contain the values pointing to theChildCLassFactoryvirtual functions, thus these functions would be called. All of this is long after invoking undefined behaviour. It is as if you are jumping with a car into a large canyon and thinking “hey, everything is driving fine” just because you have not hit the ground yet.