Re: Question on auto_ptr, Which function will call first?
James Kanze wrote:
NOTE A, obviously. The trivial reason is because that is what
the language requires. Of course, the reason the language
requires this is that it couldn't possibly work otherwise: you
have to allocate the memory for the object before you can
construct it.
I'm debugging through this code and found that *NOTE A* will called
first. Can someone explained it?
Think. What would it mean if NOTE B were called before NOTE A.
When I trace into the *new* function, the *siz* value is 4. I don't know
why it will be 4?
Because that's the size of a TraceHeap object on your machine.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
Thanks, James.
Now, I understand the important concept of " allocating before
construction ".
I think the author overload the operator new of TraceHeap is just to
show some message.
static void* operator new(size_t siz) { //*****NOTE A
void* p = ::operator new(siz);
cout << "Allocating TraceHeap object on the heap "
<< "at address " << p << endl;
return p;
}
the parameter *size_t siz* will be filled at compiling time. So, the
compiler will set the siz value to sizeof(TraceHeap).
Is my understanding right? Thanks.