将通过引用传递的值分配给成员变量(在 C++ 中)

发布于 2024-09-07 11:11:35 字数 992 浏览 6 评论 0原文

我正在尝试了解 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 技术交流群。

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

发布评论

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

评论(3

云胡 2024-09-14 11:11:35

你的代码现在就很好。 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 type C rather than C &. Your SetC takes a reference to a C, and assigns the value referred to by that reference to C::c_, so what you have is an entirely separate C object that has the same value. Since you created d with automatic storage duration in main, it and c_ which is part of it remain valid until you exit from main.

黎歌 2024-09-14 11:11:35

c 被复制到哪里了?

当您执行 c_ = c;Where was c Copy? 时,您正在对 c_ 调用 operator= ,这将复制 的内容>cc_

如果 c_ 是一个引用,则不会复制任何内容,并且程序会像您期望的那样导致错误。

Where was c copied?

When you do c_ = c;Where was c copied?, you're calling operator= on c_ which will copy the contents of c to c_.

If c_ were a also reference, nothing would be copied, and the program would cause an error like you expect it to.

错爱 2024-09-14 11:11:35

C 在这里被复制:

void setC(C &c) { c_ = c; }

如果您想存储引用,那么您的成员变量c_ 也应该是一个引用。如果您要存储引用,那么您必须小心传入变量的生命周期。

C is getting copied here:

void setC(C &c) { c_ = c; }

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.

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