 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sun Oct 29, 2006 10:11 am Post subject: partial specialization on std::vector |
|
|
i want to create a vector of pointer s.t. it can handle new and delete
but also have std::vector interface
can i implement by partial specialization and inherence like follow ?
#include <vector>
#include <algorithm>
template <typename T>
struct delete_ptr {
void operator() (T * p) {
delete p;
}
};
template<typename T>
class auto_vector<T *> : public std::vector<T *> {
public:
typedef std::vector<T *> Base;
~auto_vector() {
clear();
}
void clear() {
std::for_each(begin(), end(), delete_ptr<T>());
Base::clear();
}
void push_back(const T *p) {
T* tmp = new T(*p)
Base::push_back(tmp);
}
}; |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Sun Oct 29, 2006 10:11 am Post subject: Re: partial specialization on std::vector |
|
|
<lokchan (AT) gmail (DOT) com> wrote in message
news:1162099118.599105.315010 (AT) b28g2000cwb (DOT) googlegroups.com
| Quote: | i want to create a vector of pointer s.t. it can handle new and delete
but also have std::vector interface
can i implement by partial specialization and inherence like follow ?
#include <vector
#include <algorithm
template <typename T
struct delete_ptr {
void operator() (T * p) {
delete p;
}
};
template<typename T
class auto_vector<T *> : public std::vector<T *> {
public:
typedef std::vector<T *> Base;
~auto_vector() {
clear();
}
void clear() {
std::for_each(begin(), end(), delete_ptr<T>());
Base::clear();
}
void push_back(const T *p) {
T* tmp = new T(*p)
Base::push_back(tmp);
}
};
|
I suggest you might like to look at the Boost Pointer Containers:
http://www.boost.org/libs/ptr_container/doc/ptr_container.html
--
John Carson |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|