T
Tony Johansson
Hello!
Here I have an Handle body example and reference-counted String Class Copy
constructor and Destructor.
The class definition is called String and StringRep se below.
This class definitions below is taken from a course I was on for some time
again.
I have added this code
String()
{ myBody = new StringRep(); } which is the String constructor
Why is not the copy constructor for class StringRep called in this statement
myBody(s.myBody) that I have in the copy constructor for class String.
Can somebody explain what this code does.?
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;
}
....
....
};
class StringRep
{
private:
char* myChars;
int refCount;
StringRep(const StringRep& sr) : refCount(1)
{
//now copy the chars
......
}
friend class String;
};
//Tony
Here I have an Handle body example and reference-counted String Class Copy
constructor and Destructor.
The class definition is called String and StringRep se below.
This class definitions below is taken from a course I was on for some time
again.
I have added this code
String()
{ myBody = new StringRep(); } which is the String constructor
Why is not the copy constructor for class StringRep called in this statement
myBody(s.myBody) that I have in the copy constructor for class String.
Can somebody explain what this code does.?
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;
}
....
....
};
class StringRep
{
private:
char* myChars;
int refCount;
StringRep(const StringRep& sr) : refCount(1)
{
//now copy the chars
......
}
friend class String;
};
//Tony