2

I'm a little confused about when things are copied and when they are referenced in C++. For example I have this very simple method where the arguments are references:

void setTimeSig(const int &tsNumerator, const int &tsDenominator) {
    this->timeSigNum = tsNumerator;
    this->timeSigDenom = tsDenominator;
}

Does this mean that because I'm using references when the function where setTimeSig is finished, the object with the timeSigNum and timeSigDenom will have these two fields empty? Or is it being copied at this point: this->timeSigNum = tsNumerator;

And one more question about the same thing:

class A{
public:
    B bObject;
}
B b;
A a;
a.bObject = b;

Is bObject now referencing to b or does it contain a copy?

Any information on where or what I should read about this is much appreciated. I'm still confusing many things.

  • Reading one of [these](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should do your understanding good. – chris Jul 26 '12 at 13:59
  • Copies are made in both cases. Remember that references can only be bound to refered-to object when they're initialized (at the function call, in your case). – jrok Jul 26 '12 at 14:01

3 Answers3

5

References can be thought of as implicit pointers. While semantically they can have a more complex meaning (i.e., references are often thought of as being "bound", and can't be NULL), often the underlying compiler treats them as pointers that are automatically dereferenced. So from the compiler's perspective, your code actually looks like the following:

this->timeSigNum = *tsNumerator;
this->timeSigDenom = *tsDenominator;

So the value of the reference itself isn't being copied, but rather the value from the original variable that the reference is bound to is being copied, the same as if you had manually dereferenced an explicit pointer.

Jason
  • 31,834
  • 7
  • 59
  • 78
3

In the first example you are copying the values referenced by your function argument into your member variables.

Same thing in your second example, here you copy the values from your object b to your object a.bObject.

You are always using the assignment operator when using the operator= and the default way you do this is a so called shallow copy. When you have dynamic data in your class you need to be careful because the default one will not do since you will only copy the address of the dynamic data, and this data can be destroyed in either the original instance or the copied instance. In this case you need to do deep copying which means that you need to copy the dynamic data manually by overloading the assignment operator.

More about shallow vs deep copying here:

http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

More reading from the same source on the subject of copy/assignment oprators:

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

Bakery
  • 406
  • 1
  • 5
  • 10
0
  1. It is being copied at that point: this->timeSigNum = tsNumerator;
  2. Contains a Copy.

C++ uses references (almost)only when you explicitly declare stuff as a reference.

yuri kilochek
  • 12,709
  • 2
  • 32
  • 59