reference counting

T

Tony Johansson

Hello!

I have these two class definitions below. Now to my question.
This text is from a book that I say must be wrong.

"The use of reference counters for the classes below brings up the question
of whether the assignment statement should be shallow or deep; in other
words, should you copy reference or values?
Given the interface below, it does not matter, because the state of Point
cannot be modified."

I mean it's a big difference if you do a deep copy which mean that in this
example the left side would get brand new Point object identical to the
right side.
But in the sentence above they say it doesn't matter but is that really
true.

//Tony


class Point
{
public:
point(double=0,double=0);
virtual ~Point();
double getX() const;
double getY() const;
private:
double x_;
double_;
};

class PointCounted
{
public:
PointCounted(double=0, double=0); // creates a point
PointCounted(const PointCounted&);
PointCounted(const Point&);
PointCounted& operator=(const PointCounted&);
~PointCounted();
double getX() const;
double getY() const;
private:
Point* p; //bridge to the implementetion
int *count_; //reference counter
};
 
D

Donovan Rebbechi

Hello!

I have these two class definitions below. Now to my question.
This text is from a book that I say must be wrong.

Oh boy. Get another book. You don't want to do a deep copy assignment with a
reference counted class. Deep copy is one ownership strategy, reference counting
is another one. By the way, what book is this ?

Cheers,
 
T

Tony Johansson

Programming with design pattern!


Donovan Rebbechi said:
Oh boy. Get another book. You don't want to do a deep copy assignment with
a
reference counted class. Deep copy is one ownership strategy, reference
counting
is another one. By the way, what book is this ?

Cheers,
 
A

Andrew Koenig

I mean it's a big difference if you do a deep copy which mean that in this
example the left side would get brand new Point object identical to the
right side.

Can you show us an example of a program that can detect the difference?

For example, a program that works like this:

Point p1;
Point p2 = p1;

// Do some stuff here, and then...

if (...)
std::cout << "Shallow copy\n";
else
std::cout << "Deep copy\n";

What would you put in place of the ... to make this program work as
suggested?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top