0

Why are references not copy-able? I'm just struggling to think clearly right now. Why wouldn't you be able to copy a reference and up end with another reference to the same object? Is this not a copy of a reference?

I feel like there's something super obvious i'm missing.

codeMetis
  • 420
  • 4
  • 16
  • 1
    What do you mean with words *references not copy-able*? Here is the example of copyable reference https://stackoverflow.com/questions/22893624/why-is-the-class-holding-a-reference-copyable – 273K Mar 20 '18 at 04:47
  • 2
    Not sure what you mean here. You can't assign a reference (`int test=42; int test2 = 24; int & testr = test; testr = test2;` goes boom), but something like `int test=42; int & testr = test; int & testc = testr;` works just fine. – user4581301 Mar 20 '18 at 04:47
  • 2
    They are copyable. They're not re-assignable. – 3Dave Mar 20 '18 at 04:56
  • The super obvious thing you're missing is a [mcve] – Disillusioned Mar 20 '18 at 05:01
  • @user4581301 "goes boom" isn't terribly accurate. More like "compiles, but doesn't do what you may think". `testr = test2;` is effectively synonymous with `test = test2;`, which certainly won't go "boom". – WhozCraig Mar 20 '18 at 05:52
  • After you bind a reference, whatever you do to it, you do to the original. If you copy it, you copy the original. – Galik Mar 20 '18 at 06:08
  • 1
    @Galik "*If you copy it, you copy the original.*" - that is not true. Sure, if you read/write a value via a reference, you read/write the original. But you can certainly copy a reference without copying the original object it refers to. Unless you mean if you copy a reference, whatever you do to the copy also applies to the original reference, not the original object being referred to. – Remy Lebeau Mar 20 '18 at 07:42
  • @RemyLebeau "copying" a reference is meaningless in `C++`. They do not exist as objects in their own right like pointers do. After a reference is bound, its *identifier* simply identifies the original object. It is another name for it. So making a copy from that identifier copies the original object. – Galik Mar 20 '18 at 11:28
  • @Galik Copying references is meaningful in the sense that a reference can be use to copy initialize another one: `int i = 42; int& a = i; int& b = a;`. – juanchopanza Mar 20 '18 at 12:48
  • @juanchopanza That's not a copy. That is initializing a new reference to the original object. All the reference does is supply *type information*. References themselves are not entities to be "copied", they are merely a (type laden) *alias* to the original object. – Galik Mar 20 '18 at 13:35
  • @juanchopanza I think you may be thinking of references like pointers, which they are not. – Galik Mar 20 '18 at 13:37
  • @Galik No, I'm not doing that. – juanchopanza Mar 20 '18 at 13:39

1 Answers1

2

In c++,declaring a named variable as a reference, that is, an alias to an already-existing object or function.

That is to say,any operation on the reference equals to operations on the orinal object refereced by you.

Therefore,once you write something like:

int foo=1;
int &bar=foo;
int a;
a=bar;//it's actually copying the foo,bar is just a alias.  

So copying the reference itself is just copying the original object.

As a supplement, once there exists a class member of reference type,the implicit default copy/move assignment will be =deleted. Because if it's allowed,after the copy assignment,the class member of reference type will not chage its orinally referecing object, which is not what we actually want and probably cause ambiguity .

However ,the copy/move constructor is allowed,because they are just initializing,not copying,which does not cause ambiguity.

choxsword
  • 3,187
  • 18
  • 44