C++ has already have pointer type, and it is already very powerful, but it also have reference type, whose most work can also be done by pointer, so what's the advantage of reference type? why C++'s creator added this type into it?
-
How about using arithmetic operators through pointers? `*a = *c + *d * *c` – mfontanini Apr 24 '13 at 13:42
-
[Try asking Bjarne Stroustrup](http://www.stroustrup.com/bs_faq.html#ask). :) – jrok Apr 24 '13 at 13:45
-
The first part of the question (what's the advantage) basically has been answered [here](http://stackoverflow.com/q/57483/420683), whereas the second part (why have they been added) has been answered [here](http://stackoverflow.com/q/4716426/420683). – dyp Apr 24 '13 at 14:02
3 Answers
First of all, it indicates that the object can't be null.
Second, it cant be assigned to point to another variable.

- 12,825
- 5
- 58
- 90
The answer is "copy constructors".
C++ is a value-based language. When you say Foo x; Foo y = x;
, you need a way for y
to have the same value as x
. To do this, you need a copy constructor. The copy constructor has to take as an argument the x
object, but it can't make a copy of it, since that's the very operation we're trying to define.
So it must take the argument by reference.
(You could have made copy constructors act weirdly and take an address, but that would be unexpected and make a mess of the language, especially given its evolution from the C language. A new type feature is a much cleaner and more powerful solution.)

- 464,522
- 92
- 875
- 1,084
-
-
This is hardly the reason. The copy constructor could just as easily accept a pointer to the object to be duplicated. – davmac Apr 24 '13 at 13:44
-
Making a copy constructor take a pointer would have been unnatural and weird, and broken the "extension" quality of C++ compared to C. After all, it makes perfect sense to also have a constructor that actually takes a pointer. – Kerrek SB Apr 24 '13 at 13:45
-
I don't buy that it would be "unnatural and weird". It only seems that way because you're used to the (present) alternative. – davmac Apr 24 '13 at 13:46
-
2I don't think this answer is particular good. It singles out *one* use for references, which could also be covered by pointers, and only provides a weak subjective reasoning for references being a better choice. -1 – Apr 24 '13 at 13:46
-
1The question wasn't what references are good for, but why Bjarne added them. And this answer is as good as any to what he may have been thinking. +1. – StoryTeller - Unslander Monica Apr 24 '13 at 13:49
-
2If the copy ctor would take a pointer, the syntax would be `Foo y = &x;` or there had to be an implicit conversion. – dyp Apr 24 '13 at 13:49
-
1
-
1@devmac and an implicit conversion to pointer types will not follow the spec of `C` about pointers. Which was also a goal of `C++`. – StoryTeller - Unslander Monica Apr 24 '13 at 13:52
-
1StoryTeller, the implicit conversion only happens with copy constructors, which aren't a C feature. There wouldn't have been any breaking of C compatibility. – davmac Apr 24 '13 at 13:53
-
1Well,I think it would just look weird to have pointers behave differently, even if it is solely with a new language feature. – StoryTeller - Unslander Monica Apr 24 '13 at 13:58
reference is only referencing one object at his life it and it must have an object to reference (can't be null).
In my believe the creators of C++ was aiming to use only references (like C#) but have to allow the use of the pointers for two reasons: The references in C++ wasn't strong enough to have all the needed functionality and C++ was promised to have back comparability with C (every thing the C compile C++ compile)

- 18,955
- 12
- 67
- 94
-
References in C# (and similar things in other languages) *seem* like C++ references, but they're actually quite different. How much of this is due to the other differences in the execution model is up for debate, but in any case I doubt it's a helpful analogy. – Apr 24 '13 at 13:48
-
I agree that the references in C++ and C# implementationlly and conceptually different BUT I believe that the original intention was the same just that in C++ they have constrains which prevent them from reaching the same thing. – Roee Gavirel Apr 24 '13 at 13:52