Need idea with templates

Hi, i created smart pointers class, which can be used as simple pointer or like pointer to pointer. Everything is works(i think) but there some confusion when trying to create pointer to pointer of type T.

here is some code
1
2
3
SmartPtr< int > a(3); //synonim for int * a = new int[3];
SmartPtr< SmartPtr<int> > a1(3); //synonim for int ** a1 = new int*[3];
a1[0].allocate(10); //a1[0] = new int[10]; 


so i need some idea about swapping
SmartPtr< int * > p1;
to
SmartPtr< SmartPtr<int> > p1;

how it can be realized?
thanks for your answers!
Why?

If all you're doing is trying to ensure correct new/dereference/delete behaviour, why should you care what type it is?
Last edited on
it's only because i want to do using of my SmartPtr class more understandable. Is it possible to do what i want, if yes, what c++ features i must use to do this.
I'm not sure what you want to do. Can you explain?
SmartPtr is smart pointer class which deletes allocated memory itself.
The main problem is, when i want to create "pointer to pointer to int" using SmartPtr class, the syntacs looks very confusion, like this
SmartPtr< SmartPtr <int> > p(10);
so, i want to do something which will be more understandable, for example
SmartPtr< int* > p(10);
so how i can do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/remove_pointer.hpp>

template< typename T, bool IsPtr >
struct SmartPtrHelper_;

template< typename T >
struct SmartPtrHelper_< T, true > {
    typedef typename boost::remove_pointer<T>::type ptr_type;
};

template< typename T >
struct SmartPtrHelper_< T, false > {
    typedef T ptr_type;
};

template< typename T >
struct SmartPtr : SmartPtrHelper_< T, boost::is_pointer<T>::value >
{

};

Last edited on
Is this just for experience/practice/fun? Otherwise, what you should be doing is using boost::shared_ptr.
It appears from the usage above that OP is creating a smart pointer to an array, which cannot be done
using boost::shared_ptr, however there does exist boost::shared_array for this purpose.
Topic archived. No new replies allowed.