473,414 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

a piece of code for handle - body

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
Jul 23 '05 #1
2 1227
Tony Johansson wrote:
Hello!

[snip]
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.
It isn't. What is called is the copy constructor for the type StringRep*.
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++; }
Note that s.myBody is of type StringRep*. The newly created String object
has its own pointer to the StringRep object that presumably represents the
string and keeps track of the reference count. This pointer is initialized
from the pointer in the String object s.
[snip]

Best

Kai-Uwe Bux

PS. a) What's wrong with std::string?

PS. b) Your code is not thread safe, e.g., you cannot asume that
incrementing the reference count is atomic.
Jul 23 '05 #2
>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)
Because this is copying a pointer, not the object.
that I have in the copy constructor
for class String. Can somebody explain what this code
does.?


<snip>

Have a look at Scott Meyer's "More Effective C++", Item 29.
Jonathan

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Big Bolt | last post by:
A piece of text on my HTML page is framed with DIV tags. While I want to be able to drag/drop it, I don't want it to be selected, since it interferes with dragging. I'd like to press a mouse...
7
by: Tony Johansson | last post by:
Hello!! Assume I have a handle body pattern with classes called Handle and Body. In the Body class I store one int value for example 7 or some other integer value. In the Handle class I have...
0
by: Tony Johansson | last post by:
Hello! Here I have two classes these are called Handle and Body and a main. You have the class definition below. Some basic information. In the Handle class is there a pointer to the Body. Each...
3
by: NeverLift | last post by:
But, if it's not open, I don't want to open it . . . using window.open will open it if it doesn't exist, even if the url in that open is null (the window is then empty -- but it's open). The...
1
by: praba kar | last post by:
Dear All, I am new to python world. I have pasted my code which I used it to build rfc822 format mails for webbased mailing system task(which is like to yahoo.com web interface). Now I am...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
56
by: Rob Dob | last post by:
VS2005 is a PIECE of Garbage and is bug Ridden, I wonder how many others feel the same, I am so sorry that I have moved away from VS2003, VS2005 is unstable, and half the stuff doesn't work, ...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
3
by: andersond | last post by:
I have a PHP file that usually sends an email. Instead, it now simply displays the code beginning with line 14 (where #eol= begins). Can anybody help me with this? <?php #Change only the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.