0

As the topic says - What is the biggest problem that would occur if we dropped usage of pointers and resorted to only references in C++? Let's say we have a new that returns a reference and a delete which takes one.

nakiya
  • 14,063
  • 21
  • 79
  • 118
  • 2
    [C# has pointers.](http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx) – James McNellis Nov 05 '10 at 04:48
  • 1
    References are essentially pointers with some syntactic sugar. This notion that other languages don't have them is wrong. All imperative languages have pointers, it is the foundation on which programming is built. Usually it is memory management that gets people into trouble, not pointers themselves. – edA-qa mort-ora-y Nov 05 '10 at 08:20
  • 1
    References are not pointers (with a shiny hat). They are aliases. – Martin York Nov 05 '10 at 09:22
  • @nakiya: How is that different from pointers? You seem to be taking a very important part of C++ and throwing it away (Current references) and replacing it with a pointer (and calling it a reference). – Martin York Nov 05 '10 at 09:24

4 Answers4

13

It would make the language effectively unusable because references in C++ are not assignable.

Once you initialize a reference to refer to a given object, it can't ever refer to any other object.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 2
    @nakiya: I discussed that in detail in my answer to [Are pointers bad?](http://stackoverflow.com/questions/4074935/are-pointers-bad/4075216#4075216) Pointers are not bad. They just need to be used sparingly and carefully. – James McNellis Nov 05 '10 at 04:33
  • What if I argue that it would make the language more functional? – nakiya Nov 05 '10 at 04:34
  • Talk about pointers being evil is a slight against the language itself being reliant on them. Many other languages have "references" which are assignable, but are effectively just pointers under the hood. – John Calsbeek Nov 05 '10 at 04:34
  • 3
    @nakiya Then we would expect some pretty phenomenal evidence to support your claims – user229044 Nov 05 '10 at 04:56
  • I rest my case :). There is no connection between dropping pointers and moving closer to functional programming. In that case, Java should be a functional language. My mistake. – nakiya Nov 05 '10 at 05:09
  • 1
    Java references *are* pointers. Just because they have GC doesn't change the fact they are simply pointers. – edA-qa mort-ora-y Nov 05 '10 at 08:21
2

You can't assign new value to reference, and you can't have not initialized reference.

Abyx
  • 12,345
  • 5
  • 44
  • 76
2

You could fake any pointer arithmetic with array indexing, so there's not much that you couldn't do.

Since C++ arrays and pointers are so intertwined, to remove one effectively you'd have to also remove the other. And removing arrays has semi-obvious side effects.

One obvious problem, though: you can't store references in arrays. That would be annoying to work around.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
2

Not being able to use most C libraries would, well, suck.

(In which case, why keep using C++? ;-)