Using the reference to an object in the member initialzation list of its constructor
c++, constructor, pass-by-reference
Solution
That's fine; the member reference is initialised to refer to the object that was passed as an argument.
However, since the `Container` is still under construction, you mustn't access it in that constructor; the only thing you can do with the reference is initialise another reference.
You must also make sure that you don't use that reference after the container is destroyed. In this example, it's fine - `m_member`, and the reference it contains, are destroyed along with the container.
Problem
Is it okay to pass the reference to an object (of type) `Container` in the member initialization list of its constructor in order to initialize a member of `Container` as follows: (code on ideone). ``` #include <cstdlib> #include <iostream> struct Container; struct Member { Member( Container& container ) : m_container( container ) { } Container& m_container; }; struct Container { Container() : m_member( *this ) { } Member m_member; }; int main() { Container c; return EXIT_SUCCESS; } ``` The code compiles but I'm not sure if its standard.