Back to listC++
Chromium RefCounted Pattern
Lv.4233@mukitaro0 playsJan 2, 2026
Reference counting pattern from Chromium browser. Base class for preventing memory leaks.
preview.cpp
1template <class T>2class RefCounted {3 public:4 void AddRef() const {5 ++ref_count_;6 }7 void Release() const {8 if (--ref_count_ == 0)9 delete static_cast<const T*>(this);10 }11 private:12 mutable int ref_count_ = 0;13};Custom problems are not included in rankings