static_cast和临时创建(最终版本) [英] static_cast and temporary creation (final edition)

查看:288
本文介绍了static_cast和临时创建(最终版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先决条件:
要了解此问题,请先阅读以下问题及其答案:
Cast auto_ptr< Base> to auto_ptr< Derived>




Cast auto_ptr< Base>到auto_ptr< Derived> Steve回答说,你的static_cast会将auto_ptr复制到一个临时文件中,因此,当临时文件被复位时,资源会被销毁(在语句结束时)



我对临时创建的过程感兴趣,而调用 static_cast
我想有一个代码,我可以跟踪为了看到这种效果。
我不能使用 static_cast< auto_ptr< Circle>> ... ,因为它不能编译,所以我需要写一些模拟类,而不是 auto_ptr 并观察临时创建的过程。 p>

我也理解,临时创建与拷贝构造函数调用密切相关。
auto_ptr 的所有权丢失是通过将源的 _radius 字段设置为负值(我需要 auto_ptr 的简单逻辑模型)。



因此,我建议以下 Circle class:

  #include< iostream> 

class Shape {};

类Circle:public Shape {
double _radius;
public:
explicit Circle(double radius = .5):_radius(radius){}
Circle& operator =(Circle& circle){
_radius = circle。 _半径;
circle._radius = -1 .;
return * this;
}
Circle(Circle& circle){* this = circle; }
double GetRadius(){return _radius; }
};

int wmain(){
using namespace std;

圈c1(100),c2(200),c3(300);
c2 = c3;

Shape * s1,s2;
s1 =& c1;
wcout<< static_cast< Circle *>(s1) - > GetRadius()<< endl;

return 0;
}

好的。在这里我们可以看到所有权转让正在 c2 = c3 中发生。
但是我不能在 static_cast 中实现临时创建。



问题是:模拟临时对象创建 static_cast



我相信Steve在转换时创建了临时对象。我唯一需要的是写一个示例临时创建。这个目标有学术上的原因。



有人可以澄清如何实现Steve的回答中描述的效果,他发表在引用的主题?


<在上一个问题中, auto_ptr 是拥有所有权的类,并且将源的指针重置为null。



现在, Circle 是一个模拟所有权的类,通过将其半径重置为-1它被复制。因此,它像这样的 auto_ptr ,但不是以任何其他方式。



模拟所有权,您需要复制 Circle ,这是您在 c2 = c3 行中进行复制分配所做的。铸造 Circle * 不会复制对象,只是指针,但是转换 Circle 会复制对象:

  int main(){
Circle c1(100);
static_cast< Circle>(c1);
std :: cout<< c1.GetRadius()<< '\\\
';
}

输出为-1。



或者如果你特别想要看到它与派生类的转换:

  struct SpecialCircle:Circle {
SpecialCircle(Circle& circle):Circle(circle){}
explicit SpecialCircle(double radius = .5):Circle(radius){}
};

int main(){
SpecialCircle s1(100);
Circle& c1 = s1;
static_cast< SpecialCircle>(c1);
std :: cout<< c1.GetRadius()<< '\\\
';
}


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?

解决方案

In your previous question, auto_ptr is the class that has ownership, and resets the source's pointer to null when it is copied.

Now, Circle is a class that simulates ownership, by resetting its radius to -1 when it is copied. So it's like an auto_ptr in that way, but not in any other way.

So, to observe loss of simulated ownership you need to copy a Circle, which is what you do with copy assignment in the line c2 = c3. Casting a Circle* doesn't copy the object, just the pointer, but casting a Circle does copy the object:

int main() {
    Circle c1(100);
    static_cast<Circle>(c1);
    std::cout << c1.GetRadius() << '\n';
}

Output is -1.

Or if you specifically want to see it with a cast to a derived class:

struct SpecialCircle: Circle {
    SpecialCircle(Circle &circle) : Circle(circle) {}
    explicit SpecialCircle(double radius = .5): Circle(radius) {}
};

int main() {
    SpecialCircle s1(100);
    Circle &c1 = s1;
    static_cast<SpecialCircle>(c1);
    std::cout << c1.GetRadius() << '\n';
}

这篇关于static_cast和临时创建(最终版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆