C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Overloading operator delete

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Senthilvel Samatharman
Guest





PostPosted: Thu Mar 04, 2004 7:12 am    Post subject: Overloading operator delete Reply with quote



I am just curious about the case 3 in the follwouing program.
I understand that case 1 is the right way to overload,
while Case 2 is erroneous.

But i do think that the implementation of operator delete of Case 3 should
end up in recursion but works fine.
Am i wrong or is it the problem with the VC6 compiler?

thanks ,
Senthil.

#include <iostream>

using namespace std;

class Test
{
private:
int s;
public:
Test(){
cout<<"Test::Test()"< }


// Case 1 : This works fine
void * operator new(size_t size)
{
return ::operator new (size); // Use global operator new
}

// This also works fine
void operator delete(void* p)
{
::operator delete(p); // Global operator delete
}

/**************************************************************/

// Case 2 : This causes infinite recursion
void * operator new(size_t size)
{
return operator new (size); // Use local operator new
}


// This causes infinite recursion
void operator delete(void* p)
{
operator delete(p); // Use local operator delete
}

/**************************************************************/
// Case 3 : This causes a compile error
void * operator new(size_t size)
{
return new(size); // Use local new operator inside "operator new"
}

// Case 3: This does not cause a compile error or does not cause a
recursion.
void operator delete(void* p)
{
delete p;
}

};

int main()
{
Test* pTest = new Test;
delete pTest;

return 0;
}


Back to top
Nick Hounsome
Guest





PostPosted: Fri Mar 05, 2004 7:01 pm    Post subject: Re: Overloading operator delete Reply with quote




"Senthilvel Samatharman" <me (AT) somewhere (DOT) com> wrote

Quote:
I am just curious about the case 3 in the follwouing program.
I understand that case 1 is the right way to overload,
while Case 2 is erroneous.

But i do think that the implementation of operator delete of Case 3 should
end up in recursion but works fine.
Am i wrong or is it the problem with the VC6 compiler?

thanks ,
Senthil.

#include
using namespace std;

class Test
{
private:
int s;
public:
Test(){
cout<<"Test::Test()"< }

[snip]

// Case 3: This does not cause a compile error or does not cause a
recursion.
void operator delete(void* p)
{
delete p;

This is ok because you are deleting a void* NOT a Test*

Consider:
void operator delete(void* p)
{
void* x = new int;
delete x; // you wouldn't expect this to recurse would you
x = p;
delete x; // what's the difference? None!
delete (Test*)p; // Now THIS is recursive
}


Quote:
}

};

int main()
{
Test* pTest = new Test;
delete pTest;

return 0;
}





Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.