Hi All! I am having some troubles of understanding C++ vairable lifecycles.
I wrote a tiny application(1) to understand it, but it only complicates the things:
1. when Holder object constructed - it's private variable is automatically constructed. Is there any way not to let it be constructed?
2. just after constructing Holder I am checking address of private variable, and for example it's 20. Then I am checking address of parameter (reference of the same type) and it's 40. Then I am assigning parameter to local variable and checking local variable address - and it's still 20, but it contains real value of parameter. Explain me please how is it possible, it is not copied over to the same address because copy constructor would print it.
1. No, there isn't. However, you can make m_item a pointer and create an Item instance later when you need it. In that case don't forget to delete it in the destructor.
2. Because when you write m_item = item, the assignment operator (operator=) will be invoked, not the copy constructor. The default assignment operator creates a bitwise copy of the object, just like the default copy constructor.
There is a rule that's generally called "Law of the Big Three", see: http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29