auto_ptr 令人困惑的行为

发布于 2024-08-17 07:45:44 字数 1047 浏览 5 评论 0原文

#include<iostream>
#include<memory>
#include<stdio>

using namespace std;

class YourClass
{
   int y;
public:
   YourClass(int x) {
      y= x;
   }
};
class MyClass
{
   auto_ptr<YourClass> p;
public:
   MyClass() //:p(new YourClass(10)) 
   {
      p= (auto_ptr<YourClass>)new YourClass(10);
   }
   MyClass( const MyClass &) : p(new YourClass(10)) {}
   void show() {
      //cout<<'\n'<<p; //Was not working hence commented
      printf("%p\n",p);
   }
};

int main() {
   MyClass a;
   a.show();
   MyClass b=a;
   cout<<'\n'<<"After copying";
   a.show();//If I remove copy constructor from class this becomes NULL(the value of auto_ptr becomes NULL but if class has copy constructor it remains same(unchanged)
   b.show();//expected bahavior with copy construcotr and withought copy constructor
}

让问题更具体: 目前该类具有复制构造函数,因此 a.show() 打印的 auto_ptr 值没有问题(当第二次调用时)。它保持与启动时相同的状态)。它保持不变。 如果我从类 MyClass 中删除复制构造函数,则 a.show() (第二次调用时)打印的 auto_ptr 的值为 NULL。

#include<iostream>
#include<memory>
#include<stdio>

using namespace std;

class YourClass
{
   int y;
public:
   YourClass(int x) {
      y= x;
   }
};
class MyClass
{
   auto_ptr<YourClass> p;
public:
   MyClass() //:p(new YourClass(10)) 
   {
      p= (auto_ptr<YourClass>)new YourClass(10);
   }
   MyClass( const MyClass &) : p(new YourClass(10)) {}
   void show() {
      //cout<<'\n'<<p; //Was not working hence commented
      printf("%p\n",p);
   }
};

int main() {
   MyClass a;
   a.show();
   MyClass b=a;
   cout<<'\n'<<"After copying";
   a.show();//If I remove copy constructor from class this becomes NULL(the value of auto_ptr becomes NULL but if class has copy constructor it remains same(unchanged)
   b.show();//expected bahavior with copy construcotr and withought copy constructor
}

Making the problem more specific:
Currently the class has copy constructor so there is no problem with the value of auto_ptr printed by a.show()(when it is called second time). It remians the same as it was when it was initiazed). It remians unchanged.
If I remove the copy contructor from the class MyClass , the value of auto_ptr printed by a.show()(when it is called second time) is NULL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靖瑶 2024-08-24 07:45:44

发生的事情是由于分配或复制 auto_ptr 的奇怪(但如果您考虑一下才合理)语义,例如

auto_ptr<T> a;
auto_ptr<T> b(new T());
a = b; 

...或...

auto_ptr<T> b(new T());
auto_ptr<T> a(b);

这些将按预期将 a 设置为 b,但它们也会将 b 设置为 NULL (参见http://www.cplusplus.com/reference/std/memory /auto_ptr/auto_ptr/)。

如果您没有为 MyClass 定义复制构造函数,那么编译器将为您生成一个复制构造函数,并在复制 auto_ptr 成员时执行与上述类似的操作。因此,在调用复制构造函数后,从类复制的将具有 NULL 成员。

What's happening is due to the strange (but only justifiable if you think about it) semantics of assigning or copying an auto_ptr, e.g.

auto_ptr<T> a;
auto_ptr<T> b(new T());
a = b; 

... or ...

auto_ptr<T> b(new T());
auto_ptr<T> a(b);

These will set a to b as expected, but they will also set b to NULL (see http://www.cplusplus.com/reference/std/memory/auto_ptr/auto_ptr/).

If you don't define a copy constructor for MyClass, then the compiler will generate one for you and will do just something similar to the above when it copies the auto_ptr member. Hence the copied from class will have a NULL member after the copy constructor has been called.

待天淡蓝洁白时 2024-08-24 07:45:44

您不应该将您的类强制转换为 autoptr。我肯定知道这一点。我不太确定它想要什么语法,但它应该类似于 p = new YourClass()。

You shouldn't be casting your class to the autoptr. I know that for sure. I am not sure off the top of my head what syntax it wants but it should be something like p = new YourClass().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文