473,695 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1672
"Gonçalo Rodrigues" <op*****@mail.t elepac.pt> wrote in message
news:0s******** *************** *********@4ax.c om...
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************* **********@hotm ail.com
Above views may belong only to me.
Jul 23 '05 #2
On Sat, 19 Feb 2005 13:58:59 -0800, "Larry Brasfield"
<do************ ***********@hot mail.com> wrote:
"Gonçalo Rodrigues" <op*****@mail.t elepac.pt> wrote in message
news:0s******** *************** *********@4ax.c om...
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
7800
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 declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
6
5887
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 work fine on its own but when I tried to use say 'cout' or 'cerr' from STL the tester crashed upon termination. using some breakpoints I found out that when I make use of 'cerr' in the program, there is a call to my override of delete which do not...
2
2022
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 Select(string idName, string tableName, string whereClause);
4
2002
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 reply make sure your suggestion actually works. like a <asp:button> control have a CausesValidation property that allows it to POST a button command event to the server without causing the page to make validation on form inputs, i need to get the...
3
1404
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: function MyControl_Enter(...) handles MyControl.Enter or I can overwrite the relevant virtual method:
9
10263
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 the recursivenes. What I'm trying to say is how to override new/delete using the original global
4
11456
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 fired when I click on the delete div-button, and as a result the item is getting deleted, then is opened up for editing. How do I prevent the TR onclick from happening when the delete button is used?
6
1663
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 AuthHeaderName As String Public SessionKey As String = "Default" End Class Public Class ServiceTicket:Inherits AuthHeader
10
1197
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 do more fully, and describe a possible solution. It is still not pretty, so I would appreciate any comments. I have a base class (ClassA), which is an abstract class. Most of the methods and attributes are common to all subclasses, so there is...
0
8636
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8577
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9120
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8996
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8831
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7667
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4345
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3011
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.