Let's say I create an object:
And then put this in an object:Code:template <typename T> struct RelativeLoc { ptrdiff_t offset; RelativeLoc(T* other) { offset = reinterpret_cast<const char*>(other) - reinterpret_cast<char*>(this); } T* getOtherAddr() const { return reinterpret_cast<T*>(reinterpret_cast<const char*>(this) + offset); } }
Is this safe?Code:struct MyClass { std::vector<short> stuff; RelativeLoc<std::vector<short>> relative; MyClass(): relative(&stuff); };
The goal here is to have some other object in MyClass which always needs a reference to the "stuff" in the same object. I'd like to develop a way of automatically updating this on copies/moves which doesn't require writing an entire copy constructor for MyClass. If I can fill RelativeLoc with callbacks or something, it would fit the bill.




Reply With Quote