views:

51

answers:

3

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.

A: 

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.

sepp2k
+1  A: 

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.

Alex - Aotea Studios
@Charles the Standard says "A class object can be copied in two ways, by initialization [...], and by assignment (5.17). Conceptually, these two operations are implemented by a copy constructor (12.1) and copy assignment operator (13.5.3)."
Johannes Schaub - litb
@litb: Fair point, I withdraw my original comment and my answer.
Charles Bailey
+1  A: 

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.

Jerry Coffin