c++ - Is holding a reference to another object as a member safe in this case? -
i have few classes, each depends on using instance of object in dependency chain. looks this:
class {} ; class b { a& m_a; public: b(a& a) : m_a(a) { } }; class c { b& m_b; public: c(b& b) : m_b(b) { } };
to protect myself holding dangling references because of order of destruction, i'm holding of these in container class, this:
struct data { data() : m_b(m_a), m_c(m_b) { } m_a; b m_b; c m_c; };
assuming order of members in class data
matches dependency order, safe so? there pitfalls in holding these references i'm missing?
it's ok. class-members initialized in order, in declared, so:
m_a
initialized -> m_b
initialized reference m_a
-> m_c
initialized reference m_b
.
Comments
Post a Comment