Re: can a const pointer be deleted?
On Dec 2, 1:55 pm, abir <abirba...@gmail.com> wrote:
In the std containers erase takes a const_iterator.
This is not so.
In std containers erase member function is a non-const member
function, because it changes the container contents. It would not be
logical for that function to accept const_iterator.
I have my own container and there const_iterator is a const pointer
to the item.
I want to free the memory for the item on erase (i.e both destroy &
However deallocate function for allocator (and so delete, free etc)
takes a pointer rather than
a const pointer. Is that mean i have to cast it back to a pointer?
Make your_container::erase() accept non-const iterators.
I was looking at std::list. There even a const_iterator returns a non
const node pointer (not very sure though).
Also how this works ? does the operator delete automatically cast it
to a non const type ?
const testobj* to = new testobj(1,1);
There are two different things in C++ named delete: a) delete-
expression; b) operator delete. Here you are using delete-expression
and it ignores const-ness and volatile-ness. What delete-expression a)
does is it calls the destructor of the object and then invokes
operator delete b).
In C, while this works
char* ch = (char*)std::malloc(5);
for a const pointer i explicitly need to cast it back like,
const char* ch = (const char*)std::malloc(5);
In C++ the equivalents of malloc/free are operator new/delete, not new/
char const* c = (char const*)operator new(5);
operator delete((void*)c); // need a cast here