Topic: [Q] overloading new to create handles
Author: Giorgio Verde <verde@EMBL-heidelberg.de>
Date: 18 Jan 1995 22:07:30 GMT Raw View
Hi all!
is there a way to overload the new operator in a class
in a way that the constructor of the object gets a certain
memory pointer, but what's returned is another pointer?
Example:
struct XX;
struct XX_handle {
XX* obj;
/*...*/
XX_handle(XX* iobj):obj(iobj) { /* init handle */ }
}
struct XX {
void* operator new(size_t sz) {
char* buffer=new char[sz];
return new XX_handle((XX*)buffer));
}
XX(...some parameters...) {
this=((XX_handle*)this)->obj;
// this is old-fashioned, but does it work?
// ...do something with *this
}
}
void* XX::operator new(size_t sz) {
end Example
Q1: is this still legal? is it portable?
Q2: what's the result of new XX(parameters) ?
is it a pointer to XX_handle? or the re-assigned this pointer?
Q3: is there a more elegant way to do this?
Thanks a lot
Regards,
Giorgio Verde