oops. A typical screw-up. Sorry.
This works like overloading << for a class of your own. That is, overload rules require at least one argument of the overload to be a user-defined type. Plus, inside the overload code you will use the << of the built-in types. So your Date::operator<< really uses the << of the unsigned int to output the month , day and year.
In that spirit, your operator new (in your namespace is really an allocator for your class. Like this:
- namespace MyStuff
-
{
-
unsigned char* buffer = new unsigned char[10000];
-
class MyClass
-
{
-
public:
-
void* operator new[](size_t len, void* buffer)
-
{
-
MyClass* array = (MyClass*) new (buffer)unsigned char[len * sizeof(MyClass)];
-
for (size_t i = 0; i < len; ++i)
-
{
-
array[i].MyClass::MyClass();
-
}
-
-
return array;
-
-
}
-
MyClass()
-
{
-
-
}
-
-
-
-
};
-
-
-
}
-
-
int main()
-
{
-
MyStuff::MyClass* ptr = new (MyStuff::buffer)MyStuff::MyClass[3];
-
}
Here there is a private heap called buffer allocated in the MyStuff namespace using the operator new for unsigned char.
In MyClass there is an overload of operator new[] to allocate arrays of MyClass objects where the array is located in a buffer of unsigned char. How you manage buffer has been omitted (this is the hard part).
When you dynamically create a MyClass array it is the MyClass::operator new that is called. The overload grabs memory using the placement new version of the operator new for built-in types. In this case the array is allocated from a buffer of usigned char. No MyClass constructors are called so your MyClass operator new has to call the MyClass constructors for the array elements.
This set up assumes you have a MyClass::delete to clean up the MyClass::operator new.
You may need several MyClass::operator new functions: a) for one object, b) for an array of objects.
Finally, if you are using C++ Standard Library objects, you need to research allocators instead of just overloading operator new. They have to be written a specific way.