For example, propose map<int,void*>hold where void* is always storing pointers from classA is it safe to cast it back later via static_cast?
classA* ptr = static_cast<classA*>( holditerator->second );
the reason void* is used is because hold is member of a class defined on a header used by some cpp files which don’t know what classA is. I would have to include the header of classA definitions on these cpp files which can’t be done by many reasons.
Yes,
static_castis OK in that case and the right thing to use.I have to ask why you don’t store
classA*pointers in the first place though. If you want to put derived class pointers into it, then beware, you need to upcast/upconvert (implicitly or explicitly) the derived class pointers toclassA*before you put them into the map.But even if you put also derived class pointers into the map, a base class pointer would suffice because a derived class pointer is implicitly convertible to a base class pointer.
That can be a valid reason to prevent layering violations.
That’s most probably not necessary in your case. A forward declaration suffices. If the header knows what is put into the map, but just wants to avoid including additional headers, this is the way to go.