Prerequisities: To understand this question, please, read the following question and its answer at first: Cast auto_ptr<Base> to auto_ptr<Derived>
At Cast auto_ptr<Base> to auto_ptr<Derived> Steve answered that "Your static_cast would copy the auto_ptr to a temporary, and so aS would be reset and the resource would be destroyed when the temporary is (at the end of the statement)."
I'm interested in the process of temporary creation while static_cast
is called.
I would like to have the code that I can trace in order to see this effect.
I cannot use static_cast<auto_ptr<Circle>> ...
because it cannot be compiled, so I need to write some simulation class instead of auto_ptr
and watch the process of temporary creation.
I also understand that temporary creation is closely connected with copy constructor call.
auto_ptr
's ownership loosing is simulated with copy assignment that set the _radius
field of source to negative value (I need the simple logical model of auto_ptr
).
So, I suggest the following Circle
class:
#include <iostream>
class Shape {};
class Circle: public Shape {
double _radius;
public:
explicit Circle(double radius = .5): _radius(radius) {}
Circle &operator =(Circle &circle) {
_radius = circle._radius;
circle._radius = -1.;
return *this;
}
Circle(Circle &circle) { *this = circle; }
double GetRadius() { return _radius; }
};
int wmain() {
using namespace std;
Circle c1(100), c2(200), c3(300);
c2 = c3;
Shape *s1, s2;
s1 = &c1;
wcout << static_cast<Circle *>(s1)->GetRadius() << endl;
return 0;
}
Ok. Here we can see that "ownership transferring" is taking place in c2 = c3
.
BUT I cannot achieve temporary creation in static_cast
.
The question is: how to make a small simulation of temporary object creation while static_cast
?
I believe Steve that temporary object is created while casting. The only thing I want is to write an example that shows temporary creation. This target has academic reasons.
Can someone clarify how to achieve the effect described in Steve's answer that he posted at the referred topic?