C ++:为什么你需要引用当你有指针? [英] C++: Why do you need references when you have pointers?

查看:138
本文介绍了C ++:为什么你需要引用当你有指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++:何时使用引用与指针


我不能帮助它,但是这个根本问题在我心中:为什么C ++有引用,当你也可以做指针吗?



我知道在某些情况下,他们更安全,而且更多的是他们使代码更漂亮,但技术上没有什么区别,对吧?所以有什么情况下,我不能做一个指针和引用是必须的?

我想看看具体使用引用时的例子是不可避免的。 / p>

免责声明:

我在StackOverflow上没有找到任何答案,这是一个关于语法差异的问题。我想知道为什么C ++语言首先引入引用。

解决方案

操作符重载。使用指针通过引用传递将给你不可接受的语法。


Possible Duplicate:
C++: When to use References vs. Pointers
Could operator overloading have worked without references?

I couldn't help it, but this fundamental question was on my mind: why does C++ have references when you could do with pointers just as well?

I know that in certain situations they are slightly safer and more often than not they make the code prettier, but technically there is no difference, right? So are there any situations where I couldn't do with a pointer and a reference is a must?
I would like to see specific examples of when using references is unavoidable.

Disclaimer:
I haven't found any answers to this on StackOverflow, this is not a question about the differences in syntax. I am wondering why the C++ language introduced references in the first place.

解决方案

Operator overloading. Using pointers for "passing via reference" would give you unacceptable syntax.

这篇关于C ++:为什么你需要引用当你有指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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