472,785 Members | 1,221 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,785 software developers and data experts.

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::operator 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(Allocator * a)
{ operator delete (this, a, 0); }

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

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 3785
This was adequately replied to in microsoft.public.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***********************@hotmail.com
Above views may belong only to me.

"Douglas Peterson" <Te******@nospam.msn.com> wrote in message
news:nd********************@comcast.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
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...
4
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...
0
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...
0
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...
1
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
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...
3
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
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&...
5
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); ...
3
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.