The string Copy Constructor

Assuming you've followed this so far, you might have noticed one loose end. What if we want to pass a string as a value argument to a function? As we have seen, with the current setup bad things will happen since the compiler-generated copy constructor doesn't copy strings correctly. Well, you'll be relieved to learn that this, too, can be fixed. The answer is to implement our own version of the copy constructor. Let's take another look at the header file for our string class, in Figure 8.5.

Figure 8.5. The string class interface (code\string1.h)
 class string { public: string(); string(const string& Str); string& operator = (const string& Str); ~string(); string(char* p); private: short m_Length; char* m_Data; }; ...

Get C++: A Dialog Programming with the C++ Standard Library now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.