Suche Musterimplementierung für Smart Pointer mit Referenz Counting
-
Hallo @ all.
Ich suche so eine Art Musterimplementierung für einen Smart Pointer mit Referenz Counting.
Ich weiß, es gibt fertige Lösungen z.B. in der Boost oder Loki-Library. Aber die sind mir zu bloatig
, da blick ich nicht durch.Hat vielleicht jemand sowas selbst implementiert und könnte den Code hier reinposten? Oder kennt ihr eine Seite im Netz die einen einfach gehaltenen Code hat?
MfG, Loki
-
Hallo,
evtl. hilft dir das weiter:
http://www.awprofessional.com/content/images/020163371X/autoptr\auto_ptr.html
http://www.awprofessional.com/content/images/020163371x/sourcecode/item29.html
-
ganz alt:
template<typename Type> class RCSmartPointer { private: Type* value; unsigned* count; public: RCSmartPointer(Type* value) : value(value), count(new unsigned(1)) { assert(value); } RCSmartPointer() : value(0), count(0) { } RCSmartPointer(const RCSmartPointer<Type>& other) : value(other.value), count(other.count) { ++*count; } ~RCSmartPointer() { if(count) { --*count; if(!*count) { delete value; delete count; } } } static void Swap(RCSmartPointer<Type>& a, RCSmartPointer<Type>& b) { std::swap(a.value,b.value); std::swap(a.count,b.count); } template<typename otherType> const RCSmartPointer<Type>& operator=(const RCSmartPointer<otherType>& other) { RCSmartPointer<Type> temp(other); Swap(*this,temp); return *this; } Type* operator->() { assert(value); return value; } const Type* const operator->() const { assert(value); return value; } Type& operator*() { assert(value); return *value; } const Type& operator*() const { assert(value); return *value; } template<typename otherType> bool compare(const RCSmartPointer<otherType>& other) const { assert(value); return value==other.value; } };einer meiner ersten versuche

-
-
Shade, dein Code crasht bei
RCSmartPointer<int> sp;
RCSmartPointer<int> sp1(sp);