Why is Copy Construtor called?

I am using an example from Sams Teach Yourself C++ In One Hour a day. The instructor states that passing sayHello by value to the function UseMyString() around line 68 results in the copy constructor being called. I do not understand why. Could someone explain? Is it because sayHello is a function of MyString type? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string.h>
using namespace std;

class MyString
{
private:
   char* buffer;

public:
   
   MyString(const char* initString) // constructor
   {
      buffer = NULL;
      cout << "Default constructor: creating new MyString" << endl;
      if(initString != NULL)
      {
         buffer = new char [strlen(initString) + 1];
         strcpy(buffer, initString);

         cout << "buffer points to: 0x" << hex;
         cout << (unsigned int*)buffer << endl;
      }
   }

   MyString(const MyString& copySource) // Copy constructor
   {
      buffer = NULL;
     cout << "Copy constructor: copying from MyString" << endl;
      if(copySource.buffer != NULL)
      {
         // allocate own buffer 
         buffer = new char [strlen(copySource.buffer) + 1];

         // deep copy from the source into local buffer
         strcpy(buffer, copySource.buffer);

         cout << "buffer points to: 0x" << hex;
         cout << (unsigned int*)buffer << endl;
      }
   }

   // Destructor
   ~MyString()
   {
      cout << "Invoking destructor, clearing up" << endl;
      delete [] buffer;
   }

   int GetLength() 
   { return strlen(buffer); }

   const char* GetString()
   { return buffer; }
};

void UseMyString(MyString str)
{
   cout << "String buffer in MyString is " << str.GetLength();
   cout << " characters long" << endl;

   cout << "buffer contains: " << str.GetString() << endl;
   return;
}

int main()
{
   MyString sayHello("Hello from String Class");
   UseMyString(sayHello);

   return 0;
}
The function takes one parameter by value.

How do you think the parameter str to get its value?
Thanks for the reply. Your question is a good one for me. I realize I am not clear on that also. Does str get its value when str.GetLength() is called or earlier?
Last edited on
Lets suppose that it would get its value on line 59 due to member function call. Where would the value come from, and how?
Great point, that practical question makes me understand that part of it good. It is constructed before it hits its first line.

Q: How do you think the parameter str to get its value?
A: By a MyString object calling its constructor.

Therefore the only signature that fits is to use the Copy constructor--?
Yes, on line 69 a function is called with an argument. That argument is used to initialize the parameter str. It is like you had written:
MyString str( sayHello );

By value parameters are copies of the values that the function is called with.
That helps a lot. I totally understand now. Thank you so much
Topic archived. No new replies allowed.