Hello!!
Here I have two class definitions.
Here is the copy constructorn for class String. I know that the last part in
the initialization list is wrong which is
this part myBody(s.myBody) because this just assign a pointer to anothert
pointer. I just wonder how should this
part be written if I want to call the copy constructor in class StringRep
from the copy constructor of class String.
//Tony
String(const String& s) : myBody(s.myBody)
{ myBody->refCount; }
class StringRep
{
private:
char* myChars;
int refCount;
StringRep()
{ myChars = new char(10); }
StringRep(const StringRep& sr) : refCount(1)
{ }
friend class String;
};
class String
{
private:
StringRep* myBody;
public:
String()
{ myBody = new StringRep; }
String(const String& s) : myBody(s.myBody)
{ myBody->refCount; }
~String()
{
if (--myBody->refCount == 0)
delete myBody;
cout << myBody->refCount;
}
};