-
Syntax question
Here is some code I've read which overloads the new operator for a class, I have a couple of syntax questions about it :)
Code:
void *three_d::operator new[](size_t size)
{
void *p;
cout << "Allocating array of three_d objects.\n";
// throw an exception on failure
p = malloc(size);
if(!p) {
bad_alloc ba;
throw ba;
}
return p;
}
Now new returns a memory address (I believe) so is void* what a function must return if it is to return that?
Also, this is overloading new to allocate memory for an array of objects, but I don't see how the code would change if we were to simply overload new normally?
Sorry if these questions seem a little trivial, but they've been bugging me for a couple of days now :)
Stonehambey
-
Yes, void * is the right return for new. It is identical for new [] and new - it's no difference to the memory allocation itself, only the calling of the constructor(s).
--
Mats
-