Ok don't worrie too much about syntax, I was just writing this up to help. Here is my understanding, let me know if I'm wrong/right:
getA_Version1 will return Test::a by reference (so so far there has been no copy constuctor called). Then the copy contructor on aV1 will copy a to aV1.
getA_Version2 will copy Test::a to the return object (copy constuctor), then this object will be copied to aV2 via the copy constructor.
So if I'm correct... returning by value in this case would cause two copy operations where as returning by reference will result in one copy operation.
I'm just checking that this is right. I know there are many instances where I need to pass back by value - I'm reading effective C++ atm - great book!
UserDefinedType aV2( tt.getA_Version2() );(1)
versus UserDefinedType aV2 = tt.getA_Version2();(2)
Or above two are the same ?!?!
Technically they are different, (1) is direct-initialization and (2) is copy-initialization.
Direct-initialization is, generally, more flexible and more efficient.
Thanks for that Grey Wolf. Still why is only one copy constructor getting called when using tt.getA_Version2()? Is it compiler optimisation or something?
And throughout all our try-out "assign called!" is not output. So it means when we read C++ code with A = B we do not immediately assume an assignment will take place isn't it?
Scott Meyers Effective C++ is a really good book and it is worth to note he actually mention all these back in the early 90's. His follow up More Effective C++ is also a good read.
It is so sad to note after these two great books, he releases no more further follow ups. I wonder what he is doing now instead :P
PS Btw I remember he wrote in the Foreword some of the problems is not from him originally. Those questions were raised by his students of different classes when he conduct training and classes so in a way we could think of Effective C++ is a book that comprises of multiple authors in another sense :P