Copy constructor not being invoked

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 8
Reputation: sweeya is an unknown quantity at this point 
Solved Threads: 1
sweeya sweeya is offline Offline
Newbie Poster

Copy constructor not being invoked

 
0
  #1
Nov 30th, 2008
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.

  1. [Use language = C++]
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class String
  6. {
  7. int len;
  8. char *p;
  9.  
  10. public:
  11. String ()
  12. {
  13. cout << "default constructor " << this << endl;
  14. len = 0;
  15. p = NULL;
  16. }
  17. String (const char *s)
  18. {
  19. cout << "String constructor called " << s << endl;
  20. len = strlen(s);
  21. p = new char[len + 1];
  22. strcpy(p, s);
  23. }
  24. String (const String &s)
  25. {
  26. cout << "Copy constructor invoked " << s.p << endl;;
  27. len = s.len;
  28. p = new char[len + 1];
  29. strcpy(p, s.p);
  30. }
  31. ~String ()
  32. {
  33. cout << "destructor invoked\n";
  34. delete p;
  35. }
  36. void display(void)
  37. {
  38. cout << p << "\t address of p = " << &p << "\tobject this = " << this << endl;
  39. }
  40. void operator = (const String &s)
  41. {
  42. cout << "assignment operator invoked\n";
  43. len = s.len;
  44. p = new char[len + 1];
  45. strcpy(p, s.p);
  46. }
  47. friend String operator + (const String &s, const String &t);
  48. };
  49.  
  50. String operator + (const String &s, const String &t)
  51. {
  52. String temp;
  53.  
  54. temp.len = s.len + t.len;
  55. temp.p = new char[temp.len + 1];
  56. strcpy(temp.p, s.p);
  57. strcat(temp.p, t.p);
  58.  
  59. cout << "temp = "; temp.display();
  60. return temp;
  61. }
  62.  
  63. main()
  64. {
  65. String s1("abcdefg"), s2("hijklmn ");
  66.  
  67. String s3 = s2 + s1 ;
  68.  
  69. cout << "S1 = "; s1.display();
  70. cout << "S2 = "; s2.display();
  71. cout << "S3 = "; s3.display();
  72.  
  73. return 0;
  74. }
[/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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Copy constructor not being invoked

 
0
  #2
Nov 30th, 2008
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()).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Copy constructor not being invoked

 
0
  #3
Dec 1st, 2008
in the call to the operator+ you use [/code]

String temp;

[/code]
which invokes the default constructor.

Thanx,
Sean
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Copy constructor not being invoked

 
0
  #4
Dec 1st, 2008
Oh...

Right the compiler can avoid making the copy on return. What he said. Sorry, waas a bit distracted there...

Sean
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC