टीएल;डीआर
- समस्या: सीटीसीआई समस्या १२.९ का तकनीकी विवरण।
- दृष्टिकोण: सीटीसीआई problem १२.९: implementing a custom SmartPointer class with ref count incrementing and automatic memory deallocation.
- जटिलता: इष्टतम समय और मेमोरी संतुलन।
यह लेख सीटीसीआई समस्या १२.९ का एक स्पष्ट विवरण प्रदान करता है।
१. संदर्भ और समस्या कथन
सीटीसीआई problem १२.९: implementing a custom SmartPointer class with ref count incrementing and automatic memory deallocation.
२. कोड और कार्यान्वयन
template <typename T>
class SmartPointer {
T* ref;
unsigned* ref_count;
public:
SmartPointer(T* ptr) : ref(ptr), ref_count(new unsigned(1)) {}
~SmartPointer() {
if (--(*ref_count) == 0) {
delete ref;
delete ref_count;
}
}
};
३. सारांश और एज केसेस
हमेशा सीमांत स्थितियों और शून्य इनपुट की जांच करें।
