#include <iostream>
template <typename T>
class my_smart_ptr {
T* raw;
unsigned * p_count;
public:
my_smart_ptr(T* t):
raw(t),
p_count (new unsigned(1))
{
}
my_smart_ptr(const my_smart_ptr<T> & p):
raw(p.raw),
p_count(p.p_count)
{
++(*p_count);
}
const my_smart_ptr<T> & operator=(const my_smart_ptr<T>& p)
{
if (p == *this)
{
return *this;
}
raw = p.raw;
--(*p_count);
++(*(p.p_count));
return *this;
}
~my_smart_ptr()
{
--(*p_count);
if (*(p_count) == 0)
{
delete raw;
}
}
};
class A{
public:
A()
{
std::cout<< "A()" << std::endl;
}
~A()
{
std::cout<< "~A()" << std::endl;
}
};
int main()
{
my_smart_ptr<A> a (new A);
return 0;
}
Take the 2-minute tour
×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
|
|||||||||
|
I suppose that your class is trying to simulate a shared_ptr, however, I'm missing the part of overloading the dereference methods * and ->, because without them, your class seems useless (I can't do much with the pointed object). |
|||
|
You have an error in
It is customary for the
|
||||
|