ref counting - Scott Meyers more effective C++

C

Christopher Pisz

I got handed a project that uses ref counting from Scott Meyers "More
Effective C++." It's old. I can't use the latest standard or boost,
currently.

I wonder if there is anyone around that is intimate with those chapters
of his book. There is some confusion about where the real data goes and
if it is or is not a pointer.

The example of String and StringValue is confusing, because char * is
the data. So when trying to use the example, one wonders if the
mentality is StringValue is the data with ref counting as part of the
class or if the char * it contains is the data.

Lets say I had a class Animal for example.
Do I make:

Animal - with its data and methods
AnimalRCO : public RefCountedObject - containing a pointer to an animal
AnimalRCP - Containing the template RCO instance as a member

or

AnimalRCO : public RefCountedObject - containing all the data an Animal
would
AnimalRCP - having all the methods an Animal would

I am thinking the latter, but some colleagues are thinking the former.

or perhaps even

AnimalRCO : public RefCountedObject - containing all the data _and_
methods an Animal would
AnimalRCP - having methods that call methods on the RCO for the Animal
interface

-------------------
Also, the book doesn't seem to cover assignment operator as thoroughly
as I would like

What if an Animal or AnimalRCO has a constructor that takes an int
(perhaps for number of legs)

When I say AnimalRFP = 0; what am I really saying?
 
M

Marcel Müller

I got handed a project that uses ref counting from Scott Meyers "More
Effective C++." It's old. I can't use the latest standard or boost,
currently.
I wonder if there is anyone around that is intimate with those chapters
of his book. There is some confusion about where the real data goes and
if it is or is not a pointer.

The example of String and StringValue is confusing, because char * is
the data. So when trying to use the example, one wonders if the
mentality is StringValue is the data with ref counting as part of the
class or if the char * it contains is the data.

I don't know the book but I have implemented ref counting with char*
(and others) as data type.

Lets say I had a class Animal for example.
Do I make:

Animal - with its data and methods
AnimalRCO : public RefCountedObject - containing a pointer to an animal
AnimalRCP - Containing the template RCO instance as a member

or

AnimalRCO : public RefCountedObject - containing all the data an Animal
would
AnimalRCP - having all the methods an Animal would

I am thinking the latter, but some colleagues are thinking the former.

Both implementations are possible.
The first one is the way boost:shared_ptr works like.
The second one is concept behind boost::intrusive_ptr.

If you look about performance the additional of the first approach might
be counterproductive. Especially if you have many small Animal objects.
On the other side the intrusive concept requires changes to the memory
layout of Animal. This might be not an option if you cannot change the
type or the factory for your Animals. And it might be overhead if not
all Animal objects need their own reference counter. Think of Arrays.
or perhaps even

AnimalRCO : public RefCountedObject - containing all the data _and_
methods an Animal would
AnimalRCP - having methods that call methods on the RCO for the Animal
interface

This is indeed a third option. It makes AnimalRCP behave more like an
object rather than a pointer. But I would not recommend this for public
types AnimalRCO since it might prevent you to distinguish between
reference equality and value equality and causes other confusion.
However, it might be an option if you want to hide the reference
counting entirely from the class user. E.g. a reference counted string
class that provides value semantics from outside and copy on write
internally.
-------------------
Also, the book doesn't seem to cover assignment operator as thoroughly
as I would like

What if an Animal or AnimalRCO has a constructor that takes an int
(perhaps for number of legs)

When I say AnimalRFP = 0; what am I really saying?

This should be a syntax error unless you have gone the 3rd way.
In the other cases it should involve operator new.


Marcel
 
J

James Kanze

I got handed a project that uses ref counting from Scott Meyers "More
Effective C++." It's old. I can't use the latest standard or boost,
currently.
I wonder if there is anyone around that is intimate with those chapters
of his book. There is some confusion about where the real data goes and
if it is or is not a pointer.
The example of String and StringValue is confusing, because char * is
the data. So when trying to use the example, one wonders if the
mentality is StringValue is the data with ref counting as part of the
class or if the char * it contains is the data.
Lets say I had a class Animal for example.
Do I make:
Animal - with its data and methods
AnimalRCO : public RefCountedObject - containing a pointer to an animal
AnimalRCP - Containing the template RCO instance as a member

AnimalRCO : public RefCountedObject - containing all the data an Animal would
AnimalRCP - having all the methods an Animal would

You make Animal: public RefCountedObject; other than that,
Animal is just like it normally would be, with all its functions
and data.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top