S
Senthilvel Samatharman
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()"<<endl;
}
// Case 1 : This works fine
void * operator new(size_t size)
{
return :
perator new (size); // Use global operator new
}
// This also works fine
void operator delete(void* p)
{
:
perator 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;
}
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()"<<endl;
}
// Case 1 : This works fine
void * operator new(size_t size)
{
return :
}
// This also works fine
void operator delete(void* p)
{
:
}
/**************************************************************/
// 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;
}