Question about overriding new and delete

G

Gonçalo Rodrigues

Hi all,

I have a base class, call it Object, that implements operators new and
delete.

Now suppose there is also a class, call it Derived, deriving from
Object. It has the feature that *all* its instances are statically
allocated so I have to override new and delete.

Since *all* instances are statically allocated (and already fully
initialized by the time the program starts) I should just prevent
creation of objects from the heap by making operator new private. Is
this correct? And what about operator delete? Make it also private?

Or is there a "better way" (for suitable values of better)?

Best regards,
G. Rodrigues
 
L

Larry Brasfield

Gonçalo Rodrigues said:
Hi all, Greetings.

I have a base class, call it Object, that implements operators new and
delete.

Now suppose there is also a class, call it Derived, deriving from
Object. It has the feature that *all* its instances are statically
allocated so I have to override new and delete.

That fact does not (or should not) drive the decision to
override the allocation functions. Is there anything about
your 'Derived' class that causes it to behave incorrectly
if dynamically allocated? Is there any reason to block
dynamic creation of 'Derived' objects? If no, then I see
no reason that you have to override.
Since *all* instances are statically allocated (and already fully
initialized by the time the program starts) I should just prevent
creation of objects from the heap by making operator new private. Is
this correct?

Nothing you have stated leads me to agree with your "should".
What bad thing might happen if your "should" is violated?
And what about operator delete? Make it also private?

Except in rare circumstances, the access to those
functions should be the same.
Or is there a "better way" (for suitable values of better)?

You have not given enough information to gauge "better".
 
G

Gonçalo Rodrigues

That fact does not (or should not) drive the decision to
override the allocation functions. Is there anything about
your 'Derived' class that causes it to behave incorrectly
if dynamically allocated? Is there any reason to block
dynamic creation of 'Derived' objects? If no, then I see
no reason that you have to override.


Nothing you have stated leads me to agree with your "should".
What bad thing might happen if your "should" is violated?


Except in rare circumstances, the access to those
functions should be the same.


You have not given enough information to gauge "better".

Yes, my appologies, my description is incomplete. But actually, your
answer is not, because I can't think of anything bad happening if an
instance of Derived is allocated on the heap.

with my thanks and regards,
G. Rodrigues
 
B

Bushido Hacks

It is possible to overide the new and delete operators, but it really
would not be a wise idea.

Operators are normally declared friends.

I've found that the book "How to Program C++" by Deitel and Deitel to
explain how to overide operators very well. Stop by local library some
time and check it out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top