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:
Register(Object* pObject)
ed to an ID, data is read. Pointer members are serialized as unique IDs, so upon reading them back, I RegisterResolver(int id, Object** ppMember)
s.*ppMember
(notice the dereferencing).The problem:
Base
class be registered, however Derived**
can't be converted to Base**
.void*
(not void**
) that Derived**
/ Base**
can be both converted to, but then so can Derived*
/ 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.)
As the original issue was also concerning readability and I wanted to minimize potentially confusing parameters on the interface, I've iterated on Ben Voigt's answer and ended up with this:
template<typename T>
void RegisterResolver(int id, T** ppObject)
{
// assert(ppObject != 0);
Base* pBase(*ppObject); // not used
// implementation
}