Prerequisities: To understand this question, please read the following question and its answer first: Cast auto_ptr <Base> to auto_ptr <Derived>
In Cast auto_ptr <Base> to auto_ptr <Derived> Steve replied that "Your static_cast will copy auto_ptr to a temporary one, and therefore aS will be reset, and the resource will be destroyed if it is temporary (at the end of the expression)."
I'm interested in the temporary creation process when static_cast is static_cast . I would like to have code that I can track to see this effect. I cannot use static_cast<auto_ptr<Circle>> ... because it cannot be compiled, so I need to write some modeling class instead of auto_ptr and see the process of creating it temporarily.
I also understand that temporary creation is closely related to calling the copy constructor. auto_ptr Property identification is modeled with copies that set the _radius source _radius to a negative value (I need a simple auto_ptr boolean model).
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 see that the “transfer of ownership” occurs at c2 = c3 . BUT I cannot create a temporary creation in static_cast .
The question arises: how to do a little modeling of creating a temporary object while static_cast ?
I believe this temporary object is created during casting. The only thing I need is to write an example showing a temporary creation . This goal has academic reasons.
Can someone clarify how to achieve the effect described in the text of Steve, which he published in the mentioned topic?
source share