P
Phil Endecott
Dear All,
I'm experimenting with a class that allocates itself using a nonstandard
allocator:
class Obj {
public:
static void* operator new (size_t sz) {
return my_allocate(sz);
}
static void* operator new[] (size_t sz) {
return my_allocate(sz);
}
static void operator delete (void* p, size_t sz) {
my_deallocate(p, sz);
}
static void operator delete[] (void* p, size_t sz) {
my_deallocate(p, sz);
}
... etc ...
};
This works fine for simple cases, but I am not sure how to deal with
this use of new:
Obj* ptr = new Obj(X)
I believe that this should allocate a new Obj and invoke its
copy-constructor to initialise it with a copy of X. I think that, in
order to support this, I need to implement
static void* operator new (size_t sz, const Obj& o)
Is this right? So, what should this function do? Maybe
obj* p = my_allocate(sz);
*p = o;
return p;
But this seems all wrong. Surely the purpose of operator new is to
allocate the memory, with construction happening later. And what about
subclasses of Obj?
I think I am missing something basic here - can someone help me out?
Cheers, --Phil.
I'm experimenting with a class that allocates itself using a nonstandard
allocator:
class Obj {
public:
static void* operator new (size_t sz) {
return my_allocate(sz);
}
static void* operator new[] (size_t sz) {
return my_allocate(sz);
}
static void operator delete (void* p, size_t sz) {
my_deallocate(p, sz);
}
static void operator delete[] (void* p, size_t sz) {
my_deallocate(p, sz);
}
... etc ...
};
This works fine for simple cases, but I am not sure how to deal with
this use of new:
Obj* ptr = new Obj(X)
I believe that this should allocate a new Obj and invoke its
copy-constructor to initialise it with a copy of X. I think that, in
order to support this, I need to implement
static void* operator new (size_t sz, const Obj& o)
Is this right? So, what should this function do? Maybe
obj* p = my_allocate(sz);
*p = o;
return p;
But this seems all wrong. Surely the purpose of operator new is to
allocate the memory, with construction happening later. And what about
subclasses of Obj?
I think I am missing something basic here - can someone help me out?
Cheers, --Phil.