在将unique

编程入门 行业动态 更新时间:2024-10-22 10:45:54
在将unique_ptr分配给引用时,析构函数在成员函数之前被调用(Destructor gets called before member function when assigning unique_ptr to a reference)

我正在使用unique_ptr,我得到了一些奇怪的结果。 这是代码:

class Sniffer { public: Sniffer() { cout << "Sniffer()" << endl; s = "String!"; } void operator()() { cout << "operator()(): " << s << endl; } ~Sniffer() { cout << "~Sniffer()" << endl; } private: string s; }; int main() { cout << "Begin scope!" << endl; { Sniffer& h = *std::unique_ptr<Sniffer>(new Sniffer()); // 1 //std::unique_ptr<Sniffer> p(new Sniffer()); // 2 //Sniffer& h = *p; // 2 h(); } cout << "End scope!" << endl; return 0; }

我不明白为什么使用代码“1”,析构函数在operator()()之前调用,而“End Scope!”根本不打印。 代码不会崩溃,它会执行到最后一行,我得到的输出是:

Begin scope! Sniffer() ~Sniffer() operator()(): Press any key to continue . . .

另一方面,代码“2”表现如预期:

Begin scope! Sniffer() operator()(): String! ~Sniffer() End scope! Press any key to continue . . .

我使用引用的原因仅仅是因为我感觉h()感觉比(*p)()或p->operator()()更自然。 谢谢。

I'm using unique_ptr, and I'm getting some odd results. This is the code:

class Sniffer { public: Sniffer() { cout << "Sniffer()" << endl; s = "String!"; } void operator()() { cout << "operator()(): " << s << endl; } ~Sniffer() { cout << "~Sniffer()" << endl; } private: string s; }; int main() { cout << "Begin scope!" << endl; { Sniffer& h = *std::unique_ptr<Sniffer>(new Sniffer()); // 1 //std::unique_ptr<Sniffer> p(new Sniffer()); // 2 //Sniffer& h = *p; // 2 h(); } cout << "End scope!" << endl; return 0; }

I don't understand why with code "1", the destructor gets called before operator()(), and "End Scope!", is not printed at all. Code doesn't crash, it executes till the last line, the output I get is:

Begin scope! Sniffer() ~Sniffer() operator()(): Press any key to continue . . .

On the other side code "2" behaves as expected:

Begin scope! Sniffer() operator()(): String! ~Sniffer() End scope! Press any key to continue . . .

The reason I'm using a reference is only because I felt h() felt more natural than (*p)() or p->operator()(). Thanks.

最满意答案

在这个声明中

Sniffer& h = *std::unique_ptr<Sniffer>(new Sniffer());

创建一个std::unique_ptr<Sniffer>类型的临时对象,该对象在语句执行结束时被销毁。

因此,引用h在代码块的范围内无效。

在这个声明中

std::unique_ptr<Sniffer> p(new Sniffer()); // 2

对象p被创建并将一直存在,直到代码块结束。 所以参考h

Sniffer& h = *p;

在此范围内有效。

In this statement

Sniffer& h = *std::unique_ptr<Sniffer>(new Sniffer());

a temporary object of type std::unique_ptr<Sniffer> is created that is destroyed at the end of execution of the statement.

Thus reference h is invalid in the scope of the code block.

In this statement

std::unique_ptr<Sniffer> p(new Sniffer()); // 2

object p is created and will be alive until the end of the code block. So reference h

Sniffer& h = *p;

will be valid in this scope.

更多推荐

本文发布于:2023-08-07 05:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:unique

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!