Re: this newsroup
"Liviu" <lab2k1@gmail.c0m> wrote in message
news:39Xdp.744927$Yn5.170597@en-nntp-14.dc1.easynews.com...
"Paul" <pchristor@yahoo.co.uk> wrote...
"Liviu" <lab2k1@gmail.c0m> wrote...
"Paul" <pchristor@yahoo.co.uk> wrote...
"Liviu" <lab2k1@gmail.c0m> wrote...
There is no nsmf call on 'this' after the destructor.
For your convenience, here is the abridged core code again.
The code below is totally different from your original code.
Huh?? Are you being deceitful on some purpose, or just forgetful?
Lookup your post where you asked "Please can you clarify what exactly
you are trying to do". That was your direct reply to the code I posted,
so you can't even pretend that you didn't see it.
Now take the A::f from my code posted there, drop the comment, cout,
and assert lines - which affect neither the point nor the mechanics of
the exercise. What's left is the _exact_ code I posted now.
Original code :
if(n)
{
this->~A();
/*
note: right here 'this' points to an unitialized object,
which is legal for essentially the same reasons
why 'delete this' is allowed in member functions
*/
(n % 3) ? new(this) A : new(this) B;
f(n - 1);
It is different from code below you are now trying to post below.
void A::f(int n)
{
if(n)
{
this->~A();
A *that = (n % 3) ? new(this) A : new(this) B;
that->f(n - 1);
If 'that' is a B, a completely different function is called.
}
}
What is this supposed to prove? This certainly isn't recurrence.
Go back to my original post with the code, or any number of posts since
then where I re-copied the output lines. Focus on the first two lines:
|| A::f, object 1, this = 0012FF54
|| A::f, object 2, this = 0012FF54
The same function A::f calls itself with the same 'this' pointer. See?
Why do I want to foucs on two lines that demonstrate incrementing a static
variable?.
And what happens if a new B is allocated?
Then B::f is called virtually, as shown on line #3 of the same output.
|| B::f, object 1, this = 0012FF54
Suddenly you have a vtable scenario. The original obect was stored
on the stack [...]
Huh?? The original object was created on the heap. It can't get much
more clear than this:
No it wasn't, 'this' is on the stack.Your code calls placement new to
construct a new objects at the same location as 'this'. Which happened to be
the stack.
<original code>
(n % 3) ? new(this) A : new(this) B;
f(n - 1);
</original code>
<amended code>
A *that = (n % 3) ? new(this) A : new(this) B;
that->f(n - 1);
</amended code>
In both cases you use placment new.
What is this new code you introduce below? You never used malloc to
allocate anything.
|| void *p = malloc(max(sizeof(A), sizeof(B)));
|| A *a = new(p) A;
What you did was equivalent to:
A a;
new(&a) B;
Were you born a compulsive liar or did you grow into it?