| | |
Copy constructor not being invoked
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 1
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]
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?
C++ Syntax (Toggle Plain Text)
[Use language = C++] #include <iostream> using namespace std; class String { int len; char *p; public: String () { cout << "default constructor " << this << endl; len = 0; p = NULL; } String (const char *s) { cout << "String constructor called " << s << endl; len = strlen(s); p = new char[len + 1]; strcpy(p, s); } String (const String &s) { cout << "Copy constructor invoked " << s.p << endl;; len = s.len; p = new char[len + 1]; strcpy(p, s.p); } ~String () { cout << "destructor invoked\n"; delete p; } void display(void) { cout << p << "\t address of p = " << &p << "\tobject this = " << this << endl; } void operator = (const String &s) { cout << "assignment operator invoked\n"; len = s.len; p = new char[len + 1]; strcpy(p, s.p); } friend String operator + (const String &s, const String &t); }; String operator + (const String &s, const String &t) { String temp; temp.len = s.len + t.len; temp.p = new char[temp.len + 1]; strcpy(temp.p, s.p); strcat(temp.p, t.p); cout << "temp = "; temp.display(); return temp; } main() { String s1("abcdefg"), s2("hijklmn "); String s3 = s2 + s1 ; cout << "S1 = "; s1.display(); cout << "S2 = "; s2.display(); cout << "S3 = "; s3.display(); return 0; }
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?
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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()).
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()).
![]() |
Similar Threads
- Pass By Value, and what that really means (C++)
- Why would this excpetion handling is not working (C++)
- desktop application concerning innerclass (Java)
- Object initialisation (C++)
- vectors and classes (C++)
Other Threads in the C++ Forum
- Previous Thread: Logic error, simple stat prog
- Next Thread: registry help Win32
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





