指针是否坏? [英] Are pointers bad?

查看:79
本文介绍了指针是否坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在C ++编程,我喜欢使用指针。但似乎其他较新的语言,如Java,C#和Python不允许你显式声明指针。换句话说,你不能写 int x int * y ,并且有 x 是一个值,而 y 是任何一种语言的指针。这背后的原因是什么?

I'm programming in C++ right now, and I love using pointers. But it seems that other, newer languages like Java, C#, and Python don't allow you to explicitly declare pointers. In other words, you can't write both int x and int * y, and have x be a value while y is a pointer, in any of those languages. What is the reasoning behind this?

推荐答案

指针不错,它们容易出错。在新的语言中,他们找到了做同样的事情的方法,但是在脚下射击自己的风险较小。

Pointers aren't bad, they are just easy to get wrong. In newer languages they have found ways of doing the same things, but with less risk of shooting yourself in the foot.

指针虽然没有问题。

在你的例子中,为什么你想让x和y指向同一个内存?为什么不只是总是调用它x?

Toward your example, why would you want both x and y pointing to the same memory? Why not just always call it x?

还有一点,指针意味着你必须自己管理内存的寿命。较新的语言更喜欢使用垃圾收集来管理内存,并允许指针使这个任务变得很困难。

One more point, pointers mean that you have to manage the memory lifetime yourself. Newer languages prefer to use garbage collection to manage the memory and allowing for pointers would make that task quite difficult.

这篇关于指针是否坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆