Topic: HELP! Overloading NEW()?
Author: khl@snoopy.ncku.edu.tw (Hello)
Date: 1995/05/10 Raw View
[ Article crossposted from comp.lang.c++ ]
[ Author was Hello ]
[ Posted on Wed, 10 May 1995 06:51:05 GMT ]
Hello Netters,
I got a problem when overloading the new function ...
How can I just invoke the new in the class OBJ..
and doesn't make the call the the global overloading new function?
the code fragment is shown as below ..
Thanks!
------------------------------------------------------
khl@snoopy.ncku.edu.tw
void *operator new(size_t s)
{
void *p;
printf("My overloading new!\n");
....
p = malloc(...);
....
return p;
};
class OBJ {
....
void *operator new(size_t);
void operator delete(void*); // append to list ..
};
void* OBJ::operator new(size s)
{
if (list->empty())
return new char[BYTE_PER_BLOCK];
else {
printf("Get from list -");
return (list->retrieve());
}
}
void main()
{
OBJ *abc;
for (int i=0; i<100000; i++) {
abc = new OBJ;
delete abc;
}
}
It's result :- (try on BC++ and Zortech C++)
..
Get from list - My overloading new!
... ^^^^^^^^^^^^^^^^^^^ ??
No enough memory space.....