TITLE: Tuned, class-specific operator new and delete

(From "Effective C++" by Scott Meyers, p. 25 - 33.)


One of the things that many people don't realize about operator new is
that it's inherited by subclasses. [deleted] Most class-specific versions
of operator new are designed for a specific class, not for a class or any
of its descendents. That is, given an operator new for a class X, the 
behavior of that function is almost always carefully tuned for objects of
size sizeof(X), nothing larger and nothing smaller. Because of inheritance,
however, it is possible that the operator new in a base class will be called
to allocate memory for an object of a derived class:


void* Base :: operator new (size_t size)
{
	if (size != sizeof (Base))  		// if size is "wrong"
		return ::new char [size];	// have ::new handle request

	...					// otherwise handle request
						// here
}


void* Base :: operator delete (void* deadObject, size_t size)
{
	// send objects of "wrong" size to ::delete

	if (size != sizeof(Base)) {
		::delete [] ((char*) deadObject);
		return;
	}

	...
}