-
pointer reference
Hello
I want to pass 'this' as a reference to another class's constructor:
Code:
class A {
public:
A() : m_b(*this) { }
B m_b;
};
class B {
public:
B(A &ref) : m_ref(ref) { }
A &m_ref;
};
Is this the right way to do it?
I think the red line is critical..
Thanks for help
-
Yes, although many compilers will give you a warning since the A object won't be fully constructed at the time you pass it to B's constructor. If you only store the reference and don't actually use the A object it should be fine in practice.