473,385 Members | 1,353 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,385 software developers and data experts.

How do I call the copy constructer here.

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;
}
};
Jul 23 '05 #1
4 1138
Tony Johansson wrote:
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.
Why is that wrong?
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.
Before we continue, just a note, so that we're clear on terminology.
You _cannot_ call any constructor. Plain and simple.

That said, you _can_ construct another object [dynamically] and make
it copy-constructed from the one pointed to by 's.myBody', if you
use

myBody(new StringRep(*s.myBody))

but I am not at all convinced that it's what you should be doing.
//Tony

String(const String& s) : myBody(s.myBody)
{ myBody->refCount; }

class StringRep
{
private:
char* myChars;
int refCount;

StringRep()
{ myChars = new char(10); }
This is bogus. Should probably be

myChars = new char[10];

.. And move it to the initialiser list.

StringRep(const StringRep& sr) : refCount(1)
{ }
'myChars' is *not* being copied here. You forgot to initialise it
at all, and that means it's left _uninitialised_.

friend class String;
};

class String
{
private:
StringRep* myBody;
public:
String()
{ myBody = new StringRep; }
Again, this is much better located in the initialiser list.

String(const String& s) : myBody(s.myBody)
{ myBody->refCount; } ^^^^^^^^^^^^^^^^^
I an not sure what this statement is doing there. Is there a point
to it or is it there just for debugging purposes?
~String()
{
if (--myBody->refCount == 0)
delete myBody;
cout << myBody->refCount;
Are you sure you should be manipulating 'refCount' yourself here?
Shouldn't it be the task of 'myBody' to count its own references?
}
};


I strongly recommend you to take a good look at ref-counting string
(or anything else) implementation in the literature. Learn by using,
not by re-inventing the wheel. You can much sooner understand how
it works and even improve it when you begin from something that is
already working.

V
Jul 23 '05 #2
On 2005-05-22, Tony Johansson <jo*****************@telia.com> wrote:
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.
No, that's correct. You just assign a pointer.
I just wonder how should this
part be written if I want to call the copy constructor in class StringRep
If you want to do that, you don't need the reference count. You would do something
like

String ( const String& s) : rep (s.rep) {}

or

String ( const String& s) : rep(new StringRep(s.rep)) {}

from the copy constructor of class String.

//Tony

String(const String& s) : myBody(s.myBody)
{ myBody->refCount; }


This is wrong. You need to increase the reference count. The expression
myBody->refCount
does not increase the count or do anything.

You probably meant something like
++myBody->refCount;

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3
Victor Bazarov wrote:
String(const String& s) : myBody(s.myBody)
{ myBody->refCount; }

^^^^^^^^^^^^^^^^^
I an not sure what this statement is doing there. Is there a point
to it or is it there just for debugging purposes?


I think this should have been: myBody->refCount++;
~String()
{
if (--myBody->refCount == 0)
delete myBody;
cout << myBody->refCount;


Are you sure you should be manipulating 'refCount' yourself here?
Shouldn't it be the task of 'myBody' to count its own references?


How would it do that?

Jul 23 '05 #4
>> Are you sure you should be manipulating 'refCount' yourself here?
Shouldn't it be the task of 'myBody' to count its own references?


How would it do that?

There are only three places where manipulating with counted references takes
(shall take) places: constructors, destructors and assignment operators. The
general approach is when you have a Reference class that controlles the
counter and mutexes if needed and a CountedReference class that handles its
Reference object.
class AImp : public Reference {...};
template<class T>
CountedReference {
T* imp_;
....
};

class A {
CountedReference <AImp> imp_;
....
};

It is a common practice when neither A nor AImp take care about reference
counter, mutexes, memory pool or anything else that can take plase. Such
awareness may appear to be very expensive when you decide to switch your
objects to a memory dispatcher of your own, for example, or to add a
multithreding support.

--
Michael Kochetkov

Jul 23 '05 #5

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

Similar topics

8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
4
by: Girish Shetty | last post by:
> Hi All, > > Some Strange thing with C++ Constructer !! > I never knew that we can initialize an object / a variable more than once > using C++ Constructer till I started doing some RnD...
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
3
by: puzzlecracker | last post by:
Given, class X { //..... private: auto_ptr<Y> y; };
27
by: JoeC | last post by:
I am still working on my game and my program is getting better. Most of what I want to works. I think I am having trouble with copy constructor. Basically I want it to copy the gdata array. My...
15
by: eric dexter | last post by:
csoundgrid2.main(filename, """;<sco_header>""") isn't working like I want it to. I was trying to find out what my value for filename is but it will not print. The file I am calling...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
7
by: michael | last post by:
Hi All, I have written the following to illustrate a problem. I know I have some magic numbers etc please ignore them. What I do not follow is why the line marked results in a call to the...
1
by: Elaine121 | last post by:
Hi i'm writing a program where you have to determine the smallest, number of distinctions and the average from an array of values. in my test class i get an error that says cannot find symbol -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.