I have got a deserialization scenario for an object hierarchy, in which most objects contain pointers to other objects, but don't own them.
I am trying to implement a two-step process in which:
- Objects are created,
Register(Object* pObject)
ed to an ID, data is read. Pointer members are serialized as unique IDs, so upon reading them back, IRegisterResolver(int id, Object** ppMember)
s. - Resolvers are processed: IDs are looked up and the correct address is written to the address that is
*ppMember
(notice the dereferencing).
The problem:
- I want to enforce that only pointers to objects of or derived from a certain
Base
class be registered, howeverDerived**
can't be converted toBase**
. - I want to at least avoid ambiguity when using
void*
(notvoid**
) thatDerived**
/Base**
can be both converted to, but then so canDerived*
/Base*
.
In the following scenario:
struct A: public Serialized
{
int blah;
};
struct B: public Serialized
{
float fBlah;
A* pTarget;
};
B myB;
If the interface is RegisterResolver(int id, void* ppObject)
, there's no guarantee that client code won't pass myB.pTarget
instead of &myB.pTarget
.
What can I do to improve the [type-]safety and readability of this solution?
(The target platforms are x86 and ARM.)