将通过引用传递的值分配给成员变量(在 C++ 中)
我正在尝试了解 C++ 中的范围。请考虑以下事项:
class C
{
int i_;
public:
C() { i_ = 0;}
C(int i) { i_ = i; }
C(const C &c) {
i_ = c.i_;
cout << "C is being copied: " << i_ << endl;
}
int getI() { return i_; }
~C() {cout << "dstr: " << i_ << endl;}
};
class D
{
C c_;
public:
void setC(C &c) { c_ = c; }
int getC_I() { return c_.getI(); }
};
void Test(D &d)
{
C c(1);
d.setC(c);
//here c is going out of scope, surely it will be destroyed now?
}
int main()
{
D d;
Test(d); //this sets value of c_ to the local variable in Test.
//Surely this will be invalid when Test returns?
int ii = d.getC_I();
cout << ii << endl;
}
运行此程序输出:
dstr: 1
1
dstr: 1
显然,第一次析构函数调用发生在 Test 中,第二次调用发生在程序终止且 d 被销毁时。
所以我的问题是:c 被复制到哪里了?我的推理有问题吗?我想问的一般要点是:拥有一个获取对象引用然后将其存储在成员变量中的成员函数是否安全?
非常感谢您的帮助。
I am trying to wrap my head about scope in C++. Please consider the following:
class C
{
int i_;
public:
C() { i_ = 0;}
C(int i) { i_ = i; }
C(const C &c) {
i_ = c.i_;
cout << "C is being copied: " << i_ << endl;
}
int getI() { return i_; }
~C() {cout << "dstr: " << i_ << endl;}
};
class D
{
C c_;
public:
void setC(C &c) { c_ = c; }
int getC_I() { return c_.getI(); }
};
void Test(D &d)
{
C c(1);
d.setC(c);
//here c is going out of scope, surely it will be destroyed now?
}
int main()
{
D d;
Test(d); //this sets value of c_ to the local variable in Test.
//Surely this will be invalid when Test returns?
int ii = d.getC_I();
cout << ii << endl;
}
Running this program outputs:
dstr: 1
1
dstr: 1
Apparently, the first destructor call occurs in Test, and the other when program terminates and d is destroyed.
So my question is: Where was c copied? Is there fault with my reasoning? And general point I am trying to ask: is it safe to have a member function that takes a reference to an object and then stores it in a member variable?
Many thank for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码现在就很好。
D::c_
的类型为C
而不是C &
。您的SetC
获取对 C 的引用,并将该引用引用的值分配给C::c_
,因此您拥有的是完全独立的C
具有相同值的对象。由于您在main
中创建了具有自动存储持续时间的d
,因此它和它的一部分c_
保持有效,直到您退出main
。Your code is fine as it stands right now.
D::c_
is of typeC
rather thanC &
. YourSetC
takes a reference to a C, and assigns the value referred to by that reference toC::c_
, so what you have is an entirely separateC
object that has the same value. Since you createdd
with automatic storage duration inmain
, it andc_
which is part of it remain valid until you exit frommain
.当您执行
c_ = c;Where was c Copy?
时,您正在对c_
调用operator=
,这将复制的内容>c
到c_
。如果
c_
是一个引用,则不会复制任何内容,并且程序会像您期望的那样导致错误。When you do
c_ = c;Where was c copied?
, you're callingoperator=
onc_
which will copy the contents ofc
toc_
.If
c_
were a also reference, nothing would be copied, and the program would cause an error like you expect it to.C 在这里被复制:
如果您想存储引用,那么您的成员变量
c_
也应该是一个引用。如果您要存储引用,那么您必须小心传入变量的生命周期。C is getting copied here:
If you want to store a reference then your member variable
c_
should also be a reference. If you are storing a reference then you'll have to be careful with the lifetime of the variable you're passing in.