smart pointer class is the one that take in charge of release allocated resource when itself destroyed.
recently,i want design a smart pointer class, it take in charge of release resource allocated by new or new[].
my problem how to design destructor, you should determine the pointer ptr is pointer an array or a single object.
the structure of this smart point class may be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
template<class T>;
class smart_pointer{
public:
smart_pointer(T* p):ptr(p){};
~smart_pointer(){
//how to determine to use delete[] ptr or delete ptr;
}
//
some operator overload functions correspond to pointer
like [], *, &, ++, --,etc
//
private:
T* ptr;
}
You cannot check if pointer points to array or single value that way.
However it still worth to take a look at this. It really helps with generic programming.