By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Question about overriding new and delete

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Gonçalo Rodrigues" <op*****@mail.telepac.pt> wrote in message
news:0s********************************@4ax.com...
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".
Best regards,
G. Rodrigues


--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #2

P: n/a
On Sat, 19 Feb 2005 13:58:59 -0800, "Larry Brasfield"
<do***********************@hotmail.com> wrote:
"Gonçalo Rodrigues" <op*****@mail.telepac.pt> wrote in message
news:0s********************************@4ax.com...
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".


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
Jul 23 '05 #3

P: n/a
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.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.