views:

231

answers:

5

Amongst the programming languages I know and those I've been exposed to, C++ looks like the only one to have both pointers and references. Is it true?

+1  A: 

C# has something similar, in that is has unsafe methods which can contain pointers. Although pointers in C# are somewhat more limited.

Charles Salvia
+2  A: 

Google's Go has both. Slices are references. I think it's not as general-purpose and clean-cut as in C++, where you can often pick between a pointer and a reference yourself.

unwind
Right. But if I understand correctly, one can only take a slice of an array, not of any other type.
Didier Trosset
A: 

C has both, and although it is in many ways compatible with C++ in terms of basic syntax, it is a separate language. A question I would ask is, what are the implications of having both, and not having both?

Scottie T
C doesn't have references. And C is not a superset of C++; you've got that backwards. If anything, C is a subset of C++. (Ignoring the argument that C/C++ are two different languages entirely.)
Charles Salvia
I didn't know C doesn't have references. Learned something today. You're right, I did have that backwards.
Scottie T
Two comments: 1. C does not have references. It only has pointers. 2. C is a subset, not a superset, of C++.
Chip Uni
more correctly C and C++ share a common subset
jk
@jk: C is supposed to be a subset of C++. There was divergence but they are being brought back into line.
Martin York
c++2003 and c99 share a common subset, namely c89.
Chris Becke
You should either delete this answer or edit out the "C has both" portion.
mskfisher
@Chris Becke: except that C++ isn't a proper superset of C89. There are a few comment elements with different meanings, a few things that are allowed in C89 that C++ doesn't accept, and so on. Unless my memory is acting up ever worse than usual today, C99 isn't a proper superset of C89 either (e.g., it no longer supports implicit int).
Jerry Coffin
implict cast from void* is a good example of c89 stuff that isnt in ISO c++
jk
Yeah, C++ hasn't been a strict superset of C since C++ was standardized. And it's not going to happen either. Certain areas are being brought back into line, but not all of them.
jalf
+3  A: 

Algol 68 and Pascal certainly do. IIRC, Ada does too, though I don't remember all the details. PL/I did as well -- it may (easily) have been the first to include both.

Algol 68's references were really more like C++ pointers though. In C++, once you initialize a reference, it always refers to the same object. In Algol 68, you could "reseat" a reference, so it referred to a different object.

It's been a while since I used Pascal, but if memory serves its only use of references is for parameter passing (i.e. a var parameter passes by reference). I don't think you can create a reference other than as a parameter.

Ada allows you to mark parameters as in, out, or inout. If I recall correctly, some inout parameters are copied into the function, then copied back to the caller at the end, but others are pass by reference. As I said, I don't remember the details though.

Jerry Coffin
It looks like Algol 68 only has `ref` (that are more like pointers), but not both.
Didier Trosset
I accept Pascal, that have pointers, and references even if limited to parameter passing. Don't know about Ada.
Didier Trosset
@dtrosset: you're right, my answer was poorly phrased -- I hadn't intended to say that Algol 68 had both as separate things, but that it has references that can do most pointer-like things, so it sort of has both as a single item.
Jerry Coffin
A: 

C++ is the only language that has what C++ calls pointers and references, yes. Just like Java is the only language that has Java classes, and Python is the only language that has Python functions.

Many languages have something called references, but they're rarely the exact same thing.

C# has something called references, which doesn't behave like C++ references, and if you use unsafe code, it has something called pointers too (which again aren't defined exactly like C++ pointers)

So really, it's a meaningless question. No other language has C++'s exact features, no. But many languages have similar features under the same names.

jalf