I am currently reading More Effective C++ (1995) by Scott Meyers.
In Item 29 - Reference Counting, the author metioned that the primary benefits of using reference counting are
- (1) "Simplify Bookkeeping for Heap Objects"
- (2) Efficiency in Memory and Performance.
The book suggests that we use RCObjects to accomplish this goal. For example:
class String
{
public:
String(const char* value = "");
String& operator=(const String& rhs);
~String();
private:
// Holds a reference count and a string value
struct StringValue
{
size_t refCount;
bool shareable;
char* data;
StringValue(const char *initValue);
~StringValue();
};
// Value of this String
StringValue* value;
};
However, the first book I used to learn C++ (Beginning C++20, 6th Edition, Ivor Horton) mentioned that from C++17, we can use "static inline" variable to accomplish reference counting as well. For example:
class Test
{
public:
Test(int number)
: m_number { number}
{
++m_count;
}
~Test()
{
--m_count;
}
static std::size_t getCount()
{
return m_count;
}
private:
int m_number;
static inline std::size_t m_count = 0;
};
int main()
{
Test t1 { 1 };
Test t2 { 1 };
Test t3 { 1 };
Test t4 { 1 };
std::cout << Test::getCount(); // 4
}
I am wondering whether I should use the method provided in More Effective C++ or Beginning C++20? Or they actually accomplish different things?