ich meine eigene allocator wie so erstellt:Warum nicht von std :: erben allocator
template<typename T>
class BasicAllocator
{
public:
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef T value_type;
BasicAllocator() throw() {};
BasicAllocator (const BasicAllocator& other) throw() {};
template<typename U>
BasicAllocator (const BasicAllocator<U>& other) throw() {};
template<typename U>
BasicAllocator& operator = (const BasicAllocator<U>& other) {return *this;}
BasicAllocator<T>& operator = (const BasicAllocator& other) {return *this;}
~BasicAllocator() {}
pointer address (reference value) const {return &value;}
const_pointer address (const_reference value) const {return &value;}
pointer allocate (size_type n, const void* hint = 0) {return static_cast<pointer> (::operator new (n * sizeof (value_type)));}
void deallocate (void* ptr, size_type n) {::operator delete (static_cast<T*> (ptr));}
template<typename U, typename... Args>
void construct (U* ptr, Args&& ... args) {::new (static_cast<void*> (ptr)) U (std::forward<Args> (args)...);}
void construct (pointer ptr, const T& val) {new (static_cast<T*> (ptr)) T (val);}
template<typename U>
void destroy (U* ptr) {ptr->~U();}
void destroy (pointer ptr) {ptr->~T();}
size_type max_size() const {return std::numeric_limits<std::size_t>::max()/sizeof (T);} /**return std::size_t(-1);**/
template<typename U>
struct rebind
{
typedef BasicAllocator<U> other;
};
};
Aber ich möchte wissen, warum ich nie von std::allocator
erben sollte. Liegt es daran, dass es keinen virtuellen Destruktor gibt? Ich habe viele Beiträge gesehen, die sagen, dass man eigene erstellen und nicht erben sollte. Ich verstehe, warum wir nicht std::string
und std::vector
erben sollten, aber was ist falsch mit std::allocator
erben?
Kann ich meine Klasse oben erben? Oder brauche ich dazu einen virtuellen Destruktor?
Warum?
' new (static_cast (ptr)) 'sieht aus wie ein Tippfehler (die Funktion nimmt bereits einen' Zeiger == T * ') –
dyp