943,616 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 602
  • C++ RSS
Nov 30th, 2008
0

Copy constructor not being invoked

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
sweeya is offline Offline
8 posts
since Nov 2008
Nov 30th, 2008
0

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()).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 1st, 2008
0

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
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Dec 1st, 2008
0

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
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Logic error, simple stat prog
Next Thread in C++ Forum Timeline: registry help Win32





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC