I added to same code a function test3.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
class strings
{
char str[256];
public :
strings(const char *s)
{
strcpy(str, s);
}
strings(const strings &s)
{
cout << "copy constructor = " << s.str << endl;
strcpy(str, s.str);
}
~strings(void)
{
cout << "Destructing string " << str << endl;
}
void test1(const strings Obj1)
{
cout << "in test1 " << Obj1.str << endl;
}
strings test2(const strings Obj2)
{
cout << "in test2 " << Obj2.str << endl;
return(Obj2);
}
strings test3(void)
{
strings tmp("GoodBye");
return(tmp);
}
};
int main(int argc, char *argv[])
{
strings s1("Hellow world");
cout << "Calling test1 : " << endl;
s1.test1(s1);
cout << "\n\nCalling test2" << endl;
s1.test2(s1);
// I added a call to test3.
cout << "\n\nCalling test3" << endl;
s1 = s1.test3();
system("PAUSE");
return EXIT_SUCCESS;
}
Begin the ouput :
Calling test1 :
copy constructor = Hellow world
in test1 Hellow world
Destructing string Hellow world
Calling test2
copy constructor = Hellow world
in test2 Hellow world
copy constructor = Hellow world
Destructing string Hellow world
Destructing string Hellow world
Calling test3
Destructing string GoodBye
Press any key to continue . . .
End output.
So why is the copy constructo is not called for the return value from test3 ?
Thanks in advance.