By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,224 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,224 IT Pros & Developers. It's quick & easy.

The C++ Standard Doesn't Permit Overloading new and delete?

P: n/a
The C++ Standard Doesn't Permit Overloading new and delete?

In the 13.5 of The C++ standard (ISO/IEC 14882, 1998), I cannot find
the specification on overloading the operators new and delete; however,
many C++ books including "C++ Primer" say that the operators new and
delete can be overloaded. I wonder if this has definitive
specification? Who can tell me?

Many thanks to those who answer.

Aug 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Lighter napísal(a):
The C++ Standard Doesn't Permit Overloading new and delete?

In the 13.5 of The C++ standard (ISO/IEC 14882, 1998), I cannot find
the specification on overloading the operators new and delete; however,
many C++ books including "C++ Primer" say that the operators new and
delete can be overloaded. I wonder if this has definitive
specification? Who can tell me?

Many thanks to those who answer.
It does.
See 3.7.3 for description of new and delete.

Aug 9 '06 #2

P: n/a
Lighter wrote:
The C++ Standard Doesn't Permit Overloading new and delete?

In the 13.5 of The C++ standard (ISO/IEC 14882, 1998), I cannot find
the specification on overloading the operators new and delete; however,
many C++ books including "C++ Primer" say that the operators new and
delete can be overloaded. I wonder if this has definitive
specification? Who can tell me?

Many thanks to those who answer.
To understand this you need to find a stash of whatever special brand of
crack the designers were on when they came up with the terminology. You
can't overload the "new operator", but you can overload "operator new".

The "new operator" is what you get with a statement like:
T * pt = new T(5) ;

This has two jobs. It always does these two things, and there is
nothing you can do to change that behavior:
1. Allocate some memory.
2. Construct an object in that memory.

Step 2 is accomplished, rather obviously, by calling the object's
constructor. Step 1 is accomplished by calling .. wait for it ..
"operator new". You could call "operator new" yourself if you wanted to
get some uninitialized memory, with a statement like:
void * pmem = operator new(1024) ; // Allocate 1024 bytes of memory.

You can also replace "operator new" (overload may not be the right
terminology), for example:

void * operator new(std::size_t sz) throw(std::bad_alloc)
{
std::cout << "Global operator new called." << std::endl ;
void * p = ::malloc(sz) ;
if (!p)
throw std::bad_alloc() ;
return p ;
}
Or you can replace "operator new" for a specific class:

class T
{
public:
void * operator new(std::size_t sz) throw(std::bad_alloc)
{
std::cout << "T::operator new called." << std::endl ;
return ::operator new(sz) ; // Use global operator new.
}
} ;

There is an analogous relationship between the "delete operator" and
"operator delete".

(Note: Most of my information courtesy of Item 8 in Scott Meyers' More
Effective C++.)

--
Alan Johnson
Aug 9 '06 #3

P: n/a
On Wed, 09 Aug 2006 01:28:38 -0700, Alan Johnson wrote:
Lighter wrote:
Step 2 is accomplished, rather obviously, by calling the object's
constructor. Step 1 is accomplished by calling .. wait for it ..
"operator new". You could call "operator new" yourself if you wanted to
get some uninitialized memory, with a statement like:
void * pmem = operator new(1024) ; // Allocate 1024 bytes of memory.

You can also replace "operator new" (overload may not be the right
terminology), for example:

void * operator new(std::size_t sz) throw(std::bad_alloc)
{
std::cout << "Global operator new called." << std::endl ;
void * p = ::malloc(sz) ;
if (!p)
throw std::bad_alloc() ;
return p ;
}

So the real issue here is the meaning of overloading vs overriding. You
would not "overload" new and delete because they expect a size_t parameter
to tell how much memory to allocate, but instead you can "override" them
to use your own custom memory management, right?
Aug 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.