Amongst the programming languages I know and those I've bee开发者_JAVA百科n exposed to, C++ looks like the only one to have both pointers and references. Is it true?
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.
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.
C# has something similar, in that is has unsafe
methods which can contain pointers. Although pointers in C# are somewhat more limited.
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.
精彩评论