That's an illformed program, and you're running into undefined behavior. A class definition must be exactly the same across translation units in a program - as per 3.2 One definition rule [basic.def.odr] \6.
To hide the include of Type, you can just use the PIMPL idiom and not resort to these types of hacks.
class AImpl;
class A
{
public:
A();
~A(); //In the destructor I iterate through the map to free everything before
void function();
private:
AImpl* pImpl;
}
You just move all logic and data members inside AImpl
and keep the public interface as clean as possible. And all you need is a forward declaration of AImpl
.