Topic: interaction between operator new and constructor
Author: anhaeupl@immd4.informatik.uni-erlangen.de (Bernd Anhaeupl)
Date: Thu, 5 Aug 1993 08:36:39 GMT Raw View
Consider the following class
class obj
{
// some members
public:
void * operator new(size_t);
obj(/*other parameters*/)
// ... (other methods)
};
My problem is, that I have to distinguish in the constructors obj::obj(...),
whether obj::operator new(size_t) has been called to allocate the
space for __this__ obj. (I want to implement some reference counting
mechanism for automatic deletion of unreferenced objs). Is there a
portable way to do this? Please consider also the following situations:
1) obj may be members of other classes:
class foo
{
obj member
}
Obviously in this case obj::operator new(...) is __not__
called, therefore (also not portable) analysis, whether
the object is created on the stack or on the heap
cannot do the job.
2) obj may be constructed from other obj:
int a;
obj * b = new obj(new obj(obj(b)));
some compilers (GNU g++) collect (is this allowed by
the ARM / ANSI C++ ?) all calls to operator new(...) of
one source line before calling any constructor, so
we get some order like:
obj::new(...);
obj::new(...);
obj::obj(int);
obj::obj(obj*);
obj::obj(obj*);
instead of (like cfront):
obj::obj(int);
obj::new(...);
obj::obj(obj*);
obj::new(...);
obj::obj(obj*);
so a static flag set by the obj::operator new(...) cannot do
the job.
3) Is the following assumption for corresponding invocations of
obj::operator new(...) and obj::obj(...) guaranteed by
the ARM / ANSI C++:
obj::operator new (...) == (void*) this
If this does not need to be true, maintaining a list of
return values from obj::operator new(...) also cannot do
the job. (Please think also of multiple inheritance).
I am very interested in your opinions!