jeudi 26 février 2015

Invert pointer to member (i.e. get the address of the containing struct)

I have a struct (Member) that can only be used as data member in some other struct (Container). By convention the name of the member is always m. Is there a reliable way for the member to obtain the address of the containing struct?



template<typename Struct>
struct Member;
{
const Struct& s = ??;
// this - &Struct::m
};

struct Container
{
Member<Container> m;
};


I was hoping that maybe using the pointer to member &Container::m might help to calculate back from the address of the Member object itself?


Aucun commentaire:

Enregistrer un commentaire