Re: this newsroup
"Liviu" <lab2k1@gmail.c0m> wrote in message
news:zARcp.451242$Ph5.405532@en-nntp-07.dc1.easynews.com...
"Paul" <pchristor@yahoo.co.uk> wrote...
"Liviu" <lab2k1@gmail.c0m> wrote...
You put code forward as one recursive function operating on two
different objects. The code is actually two different functions
calling the same object/or dervied object type.
It's nothing to do with me repeating myself. Its to do with your
code using two functions to demonstrate recursion.
A::f() and B::f() are two different functions. duh.
Huh? Let me re-copy the output below. The 2nd line shows a plain
recursion A::f to A::f with the same 'this' pointing to two different
objects - object 1 and object 2.
You reconstruct the same object with placement new.
Incrementing or decrementing a static varaible doesn't change this.
"Reconstruct the same object" must be another Paul'ism ;-)
When a destructor is called, the object ends its lifetime. When
the constructor is called, a different object comes into existence.
At least that's the common usage of "same object".
According to the C++ standard an object is a region of storgage.
You create the region of storage(the object) on the free store and the
object persists until program termination.
The 3rd line shows a virtual call to
B::f again with the same 'this'. The next two lines show B::f
recursion on other different B objects, yet with the same 'this'
B::f is different function from A::f , It cannot be any clearer.
Also its the same object or derived type.
If object and subobject are not identical your code woud result in
disaster.
Not at all. It will work just as fine, and does so legally and cleanly.
Try it.
Try adding a data member to B.
If you constuct an A object on the free store and use placement new to
create a larger object at the same address something unpredictable, and
probably very bad, is going to happen.
class A{int x;};
class B:A{int y; char z;};
A a;
B* b = new(&a) B; /*oops B is larger than A*/
And so on.
Next time try to actually read or at least run the code before making
ridiculous claims about what it does.
Its quite clear what it does... It uses two functions to pass the same
object back and forth dependant on the value of a static variable.
You are confused. The type of the next object is based on (n % 3)
which is a runtime argument to the function, and has nothing to do
with the statics. Those are used just to make it easier for you to see
that the recursion goes into _different_ objects.
I'm not confused, I'm not interested tbh , its a mess and it uses two
function to pass one object back and forth. It doesn't do what you said it
does.
|| [...] A test run on my machine outputs: [...]
because the function B::f contains the code:
cout<<"B::f";
the other fucntion C::f contains the code:
cout<<"C::f";
All your code does is use two different functions to output different
strings.
You are missing the point, again. The code is a straight counterexample
to your previous assertion:
Is it hell it is two functions passing one object around. Its nothing like
a recursive function that passes a different obj per recursion.
|| I don't keep using this term, I used it to emphasise the point in the
|| example to prove that you cannot recurse a nsmf with 'this' pointing
|| to different objects per recursion
And the counterexample:
|| A::f, object 1, this = 0012FF54
|| A::f, object 2, this = 0012FF54
shows A:f calling itself recursively with the same 'this' pointing
to two different objects of type A.
Its not two different objects , its the same address , you have just
incremented a static variable.
And, as a bonus, you fail to undestand that the discussion was referring to
completely different objects of a different type or of a derived type.
As a bonus:
|| A::f, object 2, this = 0012FF54
|| B::f, object 1, this = 0012FF54
this shows how recursion works with virtual functions in a rooted
hierarchy (similar to the base/cat/dog you brought up elsewhere).
This is not recursion this is two different functions. And A is a Base class
of B so therefore objects of type A and subobjects of B types.
The objects you refer to are the same object, one is suboject to the other.
If you think you are so smart lets see some code that shows it doing
completely different object types.
It can be done, but can you do it?
..