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

Home Posts Topics Members FAQ

Overloading class new and delete operators with parameters

class Allocator
{
public:
virtual void * Alloc(size_t) = 0;
virtual void * Free(void*) = 0;
};

class Object
{
public:
Object() {}
virtual ~Object() {}

void * operator new (size_t size, Allocator * a, size_t extra)
{ return a->Alloc(size + extra); }
void operator delete (void * p, Allocator * a, size_t)
{ return a->Free(p); }
};

Object * obj = new (allocator, 10) Object;

Everything works great up to this point, my class' new overload is called
and the allocator's Alloc function is doing the allocation.

Now how do I delete the object? From what I've read, the syntax should be:
delete (obj, allocator, 0);

but this results in a compiler error: "'delete' : cannot delete objects that
are not pointers". This is because it's operating on the last parameter in
the list (the '0').

So I change it to:
delete (allocator, 0, obj);

This compiles and the destructor for Object is called, but the global delete
operator is then invoked and not the overloaded one (the parameters
'allocator' and '0' generate no code).

If I use:
delete obj;

This compiles, and it shouldn't. I should get something like
"Object::operat or delete function does not take 0 arguments". But again, the
global delete operator is getting called after the destructors (exact same
behavior as the above invocation).
Now if I remove the virtual designation from the Object's destructor (which
I can't for this application) and use "delete obj;" syntax, I do get a
compiler error: "cannot delete pointers to objects of this type; the class
has no non-placement overload...".

Which is as it should be I'd think. I want to force the delete statements to
be 'proper' placement overloads. Why does the virtual destructor modify the
compiler's behavior here?
Now I can add this function to Object:

void Delete(Allocato r * a)
{ operator delete (this, a, 0); }

And this allows you to delete obj like:
obj->Delete(allocat or);

It both compiles and executes the overloaded destructor properly. But this
defeats the purpose of having a delete operator at all. I could just skip it
and have Delete() call a->Free() (except that the compiler will complain
about not having a matching delete overload).

So what's the deal here? What is the syntax for calling the overloaded
delete operator outside of a class member function? Am I stuck making the
destructor private and forcing obj->Delete to be called?
I'm using the Microsoft Visual C++ .NET 2003 compiler.
Jul 22 '05 #1
1 3881
This was adequately replied to in microsoft.publi c.vc.language
by Doug Harrison. To the OP: Please do not multi-post.
Instead, put all targetted newsgroups into the header of a
single post. This is commonly accepted netiquette.

--
--Larry Brasfield
email: do************* **********@hotm ail.com
Above views may belong only to me.

"Douglas Peterson" <Te******@nospa m.msn.com> wrote in message
news:nd******** ************@co mcast.com...

[Cut content of post identical to post under same subject.]
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9396
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
4
2033
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the possibility of overload these operators not at the global level namespace but into different program namespaces. For instances: #include<iostream> using namespace std;
0
1347
by: Jonathan DeCarlo | last post by:
Everytime I try to instantiate an instance of an unmanaged C++ class inside of a managed C++ class, I get the following two linker errors: TestDll2 error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int)" (??2@$$FYAPAXI@Z) TestDll2 error LNK2001: unresolved external symbol "void __cdecl operator delete(void *)" (??3@$$FYAXPAX@Z)
0
902
by: Phil | last post by:
I have created a Visual C++ Class Library Project and I am using the new and delete operators to allocate and release memory. But When I try to build the Project, I receive LNK2020 error from the Linker. What Headers / Libraries do I need to include to Build this Project ? I have tried #include <new>, and linking thrownew.obj but I still get Linking Errors. Any help with this would be appreciated ...
1
1662
by: Jeff Neko | last post by:
Greetings, Is it posssible to use or recover default new/delete operators that have been overridden (by e.g. Memory Manager)??? Any advice would be much appreciated. Jeff
9
2395
by: learning | last post by:
hi I am trying to make some simple app to learn exception safety coding. I know that new and delete can throw bad_alloc but how can I force them to throw by a flag "change" at run time? I am overloading new, new, delete and delete, change is set to 1 then it calls the custom operators. If 0, it calls the c++. But how can I overload delete and delete? The original library signature has const std::nothrow_ and throw() If I overload this,...
3
1439
by: sarathy | last post by:
Hi, Need a clarification wrt new and delete operators. Consider the 2 code snippets. 1. { Object *obj = new Object(); .... obj->method1();
1
1626
by: lavanyadeep | last post by:
can u pass template class objects as parameters to friend functions of the same class?? i tried sumthin like... template<class T> class array { T a; int n; public: friend istream& operator>>(istream&,array&); friend ostream& operator<<(ostream&,array&);};
5
4126
by: cangove | last post by:
I have a class where I have defined the following overloaded methods: static void* operator new (size_t size, const std::nothrow_t&) throw (); static void* operator new (size_t size); static void
3
1475
by: simbasaurus | last post by:
Hello! I am working on a really big project, in which the global new and delete operators can be overloaded, by including the file "overloadedops.h". The graphical interface was written using wxWidgets. The program crashes with SIGSEGV because my new and operators get called by wx functions. Could anyone explain why this happens and how can i avoid it (how can i make wx functions use the normal new and delete operators)? Thank you very...
0
8392
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
8305
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
8823
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...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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
7320
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
4151
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...
1
2726
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
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.