470,815 Members | 1,154 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Q about delete

Hi all,

I am a little confused about the delete operator, so I have a
question. Suppose we have something like

class Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}

class Derived : public Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}
Derived* der = new Derived;

Base* base = der; //Implicit upcast.

delete base;
In this case, it is the static type of the pointer that matters, so it
is the Base delete operator that gets called. In fact, by the time
delete is called the object has already been "demoted" to void* -
there is no concept of dynamic type to speak of. Am I correct?

And if the answer is yes, how do I organize things such that the
Derived delete gets called?

The use case I have is of a single-root hierarchy of heap-allocated
objects handled via smart pointers. The root operator new and delete
is just a general-purpose allocator but each class can have (and it
often does) its own special allocators.

TIA, best regards,
G. Rodrigues
Jan 3 '07 #1
1 1416
Gonçalo Rodrigues wrote:
I am a little confused about the delete operator, so I have a
question. Suppose we have something like

class Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}
You need at least a semicolon, a virtual destructor (see
http://www.parashift.com/c++-faq-lit...html#faq-20.7),
a "static" on each operator, and a size_t parameter to your delete
operator. However, as _C++ Coding Standards_ Item 46 notes, if you
provide any class-specific form of new and delete, you should provide
all of them (plain, in-place, and non-throwing) since they will be
hidden otherwise and since the STL uses the in-place operator
extensively. They can be forwarding functions to the global versions
or, if the base class implements them, "using" declarations.
class Derived : public Base {
public:
void* operator new(std::size_t size);
void operator delete(void* ptr);

//The rest goes here.
}
You need a semicolon, "static" on each operator, and a size_t parameter
to your destructor, but more likely you want:

using Base::operator new;
using Base::operator delete;
Derived* der = new Derived;

Base* base = der; //Implicit upcast.

delete base;
In this case, it is the static type of the pointer that matters, so it
is the Base delete operator that gets called. In fact, by the time
delete is called the object has already been "demoted" to void* -
there is no concept of dynamic type to speak of. Am I correct?
Base's will be called, but the size_t parameter to the destructor will
be correct as long as you have a virtual destructor in your base class.
Stroustrup deals with this very example in section 15.6 of _TC++PL_ 3rd
ed.
And if the answer is yes, how do I organize things such that the
Derived delete gets called?
Delete a Derived pointer, or preferably, use the size_t parameter to
enable the Base class's delete operator to handle freeing any block
that is passed to it (the operator isn't calling the destructor after
all; it's just freeing the memory after the object has already been
destructed) and use the base class's operators in the derived class.

You might also be interested in this FAQ, which deals with memory
pooling:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

Jan 3 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave | last post: by
1 post views Thread by Nimmi Srivastav | last post: by
3 posts views Thread by Nimmi Srivastav | last post: by
1 post views Thread by Douglas Peterson | last post: by
2 posts views Thread by Dave | last post: by
3 posts views Thread by silver360 | last post: by
9 posts views Thread by rohits123 | last post: by
10 posts views Thread by jeffjohnson_alpha | last post: by
15 posts views Thread by LuB | last post: by
29 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.