![]() |
| ||
| Copy constructor not being invoked Hi i have written a code for concatenation of two strings by overloading the string operator. The program works fine but i don't understand the sequence of steps happening. [Use language = C++][/Use] The output of the above program is as follows. String constructor called abcdefg String constructor called hijklmn default constructor 0xbff1e840 temp = hijklmn adcdefg address of p = 0xbff1e844 object this = 0xbff1e840 S1 = abcdefg address of p = 0xbff1e864 object this = 0xbff1e860 S2 = hijklmn address of p = 0xbff1e854 object this = 0xbff1e850 S3 = hijklmn abcdefg address of p = 0xbff1e844 object this = 0xbff1e840 destructor invoked destructor invoked destructor invoked why is the copy constuctor not called for S3 and also why is the destructor not called for temp? |
| ||
| Re: Copy constructor not being invoked The C++ standard explicitly allows compilers to eliminate temporary objects if the only way of detecting if those temporary objects exist is to track constructor and destructor calls. A special case of this is the Return Value Optimisation (RVO), which your compiler is implementing. This optimisation basically means the compiler can avoid multiple copies if a variable within your function (eg temp within operator+()) will only be copied into a variable in the caller (eg S3 in main()). |
| ||
| Re: Copy constructor not being invoked in the call to the operator+ you use [/code] String temp; [/code] which invokes the default constructor. Thanx, Sean |
| ||
| Re: Copy constructor not being invoked Oh... Right the compiler can avoid making the copy on return. What he said. Sorry, waas a bit distracted there... Sean |
| All times are GMT -4. The time now is 4:52 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC