简体   繁体   English

C ++是同时具有指针和引用的单一语言吗?

[英]Is C++ the single language that have both pointers and references?

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. 在我所知道的和已经接触过的那些编程语言中,C ++似乎是唯一同时具有指针和引用的语言。 Is it true? 是真的吗

Algol 68 and Pascal certainly do. Algol 68和Pascal当然可以。 IIRC, Ada does too, though I don't remember all the details. IIRC,Ada也这样做,尽管我不记得所有细节。 PL/I did as well -- it may (easily) have been the first to include both. PL / I也是如此-它可能(很容易)是第一个将两者都包括在内的。

Algol 68's references were really more like C++ pointers though. Algol 68的引用实际上更像是C ++指针。 In C++, once you initialize a reference, it always refers to the same object. 在C ++中,一旦初始化引用,它就始终引用同一对象。 In Algol 68, you could "reseat" a reference, so it referred to a different object. 在Algol 68中,您可以“重置”引用,因此它引用了另一个对象。

It's been a while since I used Pascal, but if memory serves its only use of references is for parameter passing (ie a var parameter passes by reference). 自从我使用Pascal以来已经有一段时间了,但是如果内存服务了,它唯一的引用就是用于参数传递(即var参数通过引用传递)。 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 . Ada允许您将参数标记为inoutinout 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. 如果我没记错的话,一些inout参数将被复制到函数中,然后最后复制回到调用方,而其他参数将通过引用传递。 As I said, I don't remember the details though. 正如我所说,我不记得具体细节。

Google's Go has both. Google的Go兼具两者。 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 ++那样通用和简洁,在C ++中您经常可以自己在指针和引用之间进行选择。

C# has something similar, in that is has unsafe methods which can contain pointers. C#具有类似的特性,即具有unsafe方法,其中可能包含指针。 Although pointers in C# are somewhat more limited. 尽管C#中的指针受到更多限制。

C++ is the only language that has what C++ calls pointers and references, yes. C ++是唯一具有C ++称为指针和引用的语言,是的。 Just like Java is the only language that has Java classes, and Python is the only language that has Python functions. 就像Java是唯一具有Java类的语言,Python是唯一具有Python函数的语言一样。

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) C#有一个称为引用的东西,它的行为不像C ++引用,而且如果您使用不安全的代码,它也有一个称为指针的东西(同样,它们的定义与C ++指针并不完全一样)

So really, it's a meaningless question. 所以说真的,这是一个毫无意义的问题。 No other language has C++'s exact features, no. 没有其他语言具有C ++的确切功能,不是。 But many languages have similar features under the same names. 但是许多语言以相同的名称具有相似的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM