472,374 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

Question about overriding new and delete

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
3 1608
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
6
by: shoosh | last post by:
hi for my application under VC6 I need to implement my own memory manager which overrides the global new and delete operators and which Do Not use the normal free() and malloc(). it seemed to...
2
by: byoukstetter | last post by:
So, I have an interface with several overriding methods: using System; using System.Collections.Specialized; namespace some.name.space { public interface IVrsPersistenceProvider { string...
4
by: z. f. | last post by:
Hi, i stated that this is an advanced question because i have a post from few days ago that i received answers to with suggestions that looked good but did not work, so please if you post a...
3
by: James Ramaley | last post by:
I have created a VB.NET User Control which inherits from TextBox. I have a general question regarding how functionality should be overwritten: I can either write a method which handles an event:...
9
by: groleo | last post by:
Hi list. Simple question: is it possible to override the global new/delete operators, without using malloc/free? I mean something in the ideea of the code below, which doesnt work cause of...
4
by: tomlong | last post by:
Hi, I have a tabular form that each TR has an onclick event to edit the row. I also have a div in one of the cells on each row that is a delete button. My problem is that both onclicks are being...
6
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
10
by: Frank Millman | last post by:
Hi all I recently posted a question about subclassing. I did not explain my full requirement very clearly, and my proposed solution was not pretty. I will attempt to explain what I am trying to...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.