473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1655
"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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.