参考参数寿命
假设如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
_myPrivate
不是引用,因此在赋值_myPrivate = param
中,其值将从任何引用param<复制过来/code> 指向,在本例中是
some_function()
中的局部变量p
。因此,如果
ParamClass
的赋值运算符正确实现,那么代码应该没问题。有了上述警告,是的。但准确地说,
_myPrivate
不能用于访问p
的副本;它是一个变量,包含(现已消失的)p
中的数据副本。Since
_myPrivate
is not a reference, in the assignment_myPrivate = param
, its value will be copied over from whatever the referenceparam
points to, which in this case is the local variablep
insome_function()
.So if the assignment operator for
ParamClass
is implemented correctly, the code should be fine.With the above caveat, yes. But to be precise,
_myPrivate
can not be used to access a copy ofp
; it is a variable containing a copy of the data in (the now extinct)p
.在
myMethod
中,您调用ParamClass
的赋值运算符,默认情况下它会按位复制对象(您可以定义自己的运算符)。因此,您创建了 p 的副本,该副本将可供访问in
myMethod
you invoke the assignment operator ofParamClass
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引用就像对象的别名。该引用没有自己的生命周期。要考虑的生命周期是引用对象的生命周期。
在您的示例中, _myPrivate 是一个对象,因此运算符 = 将复制引用传递的对象 p。 p 将被销毁,参数引用将不会引用任何内容,但 _myPrivate 作为副本就可以了。
如果 _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 :
In this case, you end up with a "dangled" reference : Undefined behavior :)
my2c
看一下:
_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 byparam
into members of_myPrivate
. Whensome_function
returns,p
is moved from stack - it disappears. But_myPrivate
now contains copies ofp
's members' values.If
ParamClass
has members that are pointers to dynamically allocated memory, you must make sure thatParamClass::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!