+ { return RefPtr<T>(dynamic_cast<T *>(p.data), p.counts); }
+
+private:
+ void incref() { if(counts) ++counts->count; }
+ void decref();
+};
+
+
+template<typename T>
+class WeakPtr
+{
+ template<typename U> friend class RefPtr;
+ template<typename U> friend class WeakPtr;
+
+private:
+ T *data = nullptr;
+ RefCounts *counts = nullptr;
+
+public:
+ WeakPtr() = default;
+private:
+ WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : nullptr) { incref(); }
+
+public:
+ WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); }
+
+ template<typename U>
+ WeakPtr(const WeakPtr<U> &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); }
+
+ template<typename U>
+ WeakPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
+
+ WeakPtr &operator=(const WeakPtr &p) { return assign(p); }
+
+ template<typename U>
+ WeakPtr &operator=(const WeakPtr<U> &p) { return assign(p); }
+
+ template<typename U>
+ WeakPtr &operator=(const RefPtr<U> &p) { return assign(WeakPtr(p)); }