参考参数寿命

发布于 2024-10-15 04:44:48 字数 392 浏览 5 评论 0原文

假设如下:

class ParamClass {...};

class MyObject {
public:
    void myMethod(ParamClass const& param) { _myPrivate = param; }

private:
    ParamClass _myPrivate;
}

[...]

MyObject obj;

void some_function(void)
{
    ParamClass p(...);
    obj.myMethod(p);
}

在对象 p 的生命周期结束时 _myPrivate 会发生什么? 编辑:我仍然可以使用 _myPrivate 访问对象 p 的副本吗?

谢谢!

Given the following:

class ParamClass {...};

class MyObject {
public:
    void myMethod(ParamClass const& param) { _myPrivate = param; }

private:
    ParamClass _myPrivate;
}

[...]

MyObject obj;

void some_function(void)
{
    ParamClass p(...);
    obj.myMethod(p);
}

What will happen to _myPrivate at the end of the lifetime of object p?
EDIT: will I still be able to use _myPrivate to access a copy of object p?

Thanks!

Dan

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

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

发布评论

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

评论(4

哭了丶谁疼 2024-10-22 04:44:48

由于_myPrivate不是引用,因此在赋值_myPrivate = param中,其值将从任何引用param<复制过来/code> 指向,在本例中是 some_function() 中的局部变量 p

因此,如果 ParamClass 的赋值运算符正确实现,那么代码应该没问题。

我仍然可以使用 _myPrivate 访问对象 p 的副本吗?

有了上述警告,是的。但准确地说,_myPrivate 不能用于访问 p 的副本;它一个变量,包含(现已消失的)p 中的数据副本。

Since _myPrivate is not a reference, in the assignment _myPrivate = param, its value will be copied over from whatever the reference param points to, which in this case is the local variable p in some_function().

So if the assignment operator for ParamClass is implemented correctly, the code should be fine.

will I still be able to use _myPrivate to access a copy of object p?

With the above caveat, yes. But to be precise, _myPrivate can not be used to access a copy of p; it is a variable containing a copy of the data in (the now extinct) p.

七度光 2024-10-22 04:44:48

myMethod 中,您调用 ParamClass 的赋值运算符,默认情况下它会按位复制对象(您可以定义自己的运算符)。因此,您创建了 p 的副本,该副本将可供访问

in myMethod you invoke the assignment operator of ParamClass which by default makes a bitwise copy of the object (you can define your own operator). So you create a copy of p, which will be accessible

花辞树 2024-10-22 04:44:48

引用就像对象的别名。该引用没有自己的生命周期。要考虑的生命周期是引用对象的生命周期。

在您的示例中, _myPrivate 是一个对象,因此运算符 = 将复制引用传递的对象 p。 p 将被销毁,参数引用将不会引用任何内容,但 _myPrivate 作为副本就可以了。

如果 _myPrivate 声明为:

ParamObject& _myPrivate;

在这种情况下,您最终会得到一个“悬空”引用:未定义的行为:)

my2c,这将是一个问题

A reference is like an alias to an object. The reference has no lifetime of its own. The lifetime to consider is the lifetime of the object referenced.

In your example, _myPrivate is an object so the operator= will copy the reference-passed objet p. p will be destroyed, and the parameter reference will reference nothing, but _myPrivate, as a copy will be OK.

It would be a problem if _myPrivate was declared as :

ParamObject& _myPrivate;

In this case, you end up with a "dangled" reference : Undefined behavior :)

my2c

小傻瓜 2024-10-22 04:44:48

看一下:

_myPrivate = param;

在此语句中,赋值运算符 (ParamClass::operator=) 复制 param< 引用的对象的每个成员的值/code> 进入 _myPrivate 的成员。当some_function返回时,p从堆栈中移出 - 它消失了。但 _myPrivate 现在包含 p 成员值的副本。

如果 ParamClass 的成员是指向动态分配内存的指针,则必须确保 ParamClass::operator= 执行深复制,否则可能会存在悬空指针问题 - ParamClass 的析构函数可能会释放该内存,但 _myPrivate 将有一个仍指向它的成员!

Take a look at:

_myPrivate = param;

In this statement assignment operator (ParamClass::operator=) copies values of each member of the object referred by param into members of _myPrivate. When some_function returns, p is moved from stack - it disappears. But _myPrivate now contains copies of p's members' values.

If ParamClass has members that are pointers to dynamically allocated memory, you must make sure that ParamClass::operator= performs deep copy, otherwise you might have problems with dangling pointers - ParamClass's destructor might free that memory but _myPrivate will have a member that still points to it!

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