Assigning value passed by reference to a member variable (in 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;