Reference Counting

The reason we have to set m_Count to 1 in the UndatedStockItem variable pointed to by m_Worker is that we're going to keep track of the number of StockItems that are using that UndatedStockItem worker object, rather than copy the worker object every time we copy a StockItem that points to it, as we did with our string class. This approach to sharing an object is called reference counting.

The general idea of reference counting is fairly simple, as most great ideas are (after you understand them, at least). It's inefficient to copy a lot of data whenever we set one variable to the same value as that of another; copying a pointer to the data is much easier. Even so, we have to consider how we will know when we can delete the ...

Get C++: A Dialog Programming with the C++ Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.