Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading operator new?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I've got a header file which I use to fix Microsoft's new operator so that it will actually throw a bad_alloc exception like it is supposed to, but I can't get it to compile properly. Here is the interface:
Code:
#ifndef OPERATOR_NEW_HEADER_FILE
#define OPERATOR_NEW_HEADER_FILE

#include <new>

#ifdef WIN32

#pragma warning(disable: 4290)	// C++ Exception Specification ignored.


void* operator new( size_t  size ) throw( std::bad_alloc );
void  operator delete( void*  pObject ) throw();
void* operator new[]( size_t  size ) throw( std::bad_alloc );
void  operator delete[]( void*  pObject ) throw();
void* operator new( size_t  size, const  std::nothrow_t& ) throw();
void  operator delete( void*  pObject, const std::nothrow_t& ) throw();
void* operator new[]( size_t  size, const std::nothrow_t& ) throw();
void  operator delete[]( void*  pObject, const std::nothrow_t& ) throw();

#endif	// WIN32

#endif	// OPERATOR_NEW_HEADER_FILE
When I compile, I get these errors:
"void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined"
"void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined"

If I remove the first (non-array) new and delete functions, it will compile without any problems. Why can't I overload them?
 
You don't need to declare the prototypes: just define what you want it to do. Defining the fact that it throws an exception is just for documentation purposes: it doesn't do anything on the MS compiler. If you also use Unix on Sun, it doesn't do anything either.
 
Thanks, but I still get the error whether I use these prototypes or if I define them without prototypes... :(
It's only the first 2 functions that the compiler is complaining about.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top