指针和引用参数之间的区别?
这些是否相同:
int foo(bar* p) {
return p->someInt();
}
和
int foo(bar& r) {
return r.someInt();
}
忽略空指针的可能性。 无论 someInt()
是虚拟的还是传递一个 bar
或 bar
的子类,这两个函数在功能上是否相同?
这个切片有什么作用吗:
bar& ref = *ptr_to_bar;
Are these the same:
int foo(bar* p) {
return p->someInt();
}
and
int foo(bar& r) {
return r.someInt();
}
Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt()
is virtual or if they are passed a bar
or a subclass of bar
?
Does this slice anything:
bar& ref = *ptr_to_bar;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
标准中有意未指定使用指针实现的 C++ 引用。 引用更像是变量的“同义词”,而不是指向变量的指针。 当可能意识到指针在某些情况下会显得过分时,这种语义为编译器提供了一些可能的优化。
还有一些差异:
这是一个至关重要的区别
您更喜欢其中一个的主要原因
其他。
指针,你得到的地址
指针变量。 当您采取
引用的地址,你得到
变量的地址是
提到的。
C++ references are intentionally not specified in the standard to be implemented using pointers. A reference is more like a "synonym" to a variable than a pointer to it. This semantics opens some possible optimizations for the compiler when it's possible to realize that a pointer would be an overkill in some situations.
A few more differences:
This is a crucial difference and the
main reason you'd prefer one over the
other.
pointer, you get the address of the
pointer variable. When you take the
address of a reference, you get the
address of the variable being
referred to.
忽略每一个语法糖和可以用一个而不是另一个来完成的可能性以及其他答案(其他问题)中解释的指针和引用之间的差异......是的,这两个在功能上完全相同! 两者都可以很好地调用函数并处理虚函数。
不,你的线不会切片。 它只是将引用直接绑定到指针指向的对象。
关于为什么要使用其中一个而不是另一个的一些问题:
我不会自己尝试找出差异,而是将您委托给那些您想知道的人。
Ignoring every syntactic sugar and possibilities that can be done with the one and not with the other and difference between pointers and references explained in other answers (to other questions) ... Yeah those two are functionally exactly the same! Both call the function and both handle virtual functions equally well.
And no, your line does not slice. It's just binding the reference directly to the object pointed to by a pointer.
Some questions on why you would want to use one over the other:
Instead of trying to come up with differences myself, i delegate you to those in case you want to know.
引用是一个常量指针,即,您不能更改引用以引用其他对象。 如果更改,引用对象的值也会更改。
例如:
Reference is a constant pointer i.e., you can't change the reference to refer to other object. If you change, value of the referring object changes.
For Ex:
是的,它们在功能上是相同的。 由于引用需要您在使用它之前将其设置为对象,因此您不必处理空指针或指向无效内存的指针。
查看语义差异也很重要:
Yes they are functionally identical. Since a reference will require you to set it to an object before using it, you wont have to deal with null-pointers or pointers to invalid memory.
It is also important to see the semantical difference:
我已经很长时间没有使用 C++ 了,所以我什至不会尝试真正回答你的问题(抱歉); 然而,Eric Lippert 刚刚发布了一篇优秀文章 关于我认为应该向您指出的指针/引用。
I haven't used C++ in a long time, so I'm not even going to attempt to really answer your question (sorry); However, Eric Lippert just posted an excellent article about pointers/references that I figured I'd point you to.
不确定是否有人回答了隐藏在底部的关于切片的第二个问题...不,这不会导致切片。
切片是指将派生对象分配(复制)到基类对象时——派生类的特化被“切片”。 请注意,我说对象被复制,我们不是在谈论复制/分配指针,而是在谈论对象本身。
在你的例子中,这没有发生。 您只是取消引用指向 Bar 对象的指针(从而生成 Bar 对象),该指针被用作引用初始化中的右值。 不确定我的术语是否正确......
Not sure if anyone answered your 2nd question hidden at the bottom about slicing... no that won't cause slicing.
Slicing is when a derived object is assigned (copied) to a base class object -- the derived class's specialization is "sliced" off. Note that I said the object is copied, we're not talking about pointers being copied/assigned, but the objects themselves.
In your example, that's not happening. You're just de-referencing a pointer to a Bar object (thereby resulting in a Bar object) being used as the rvalue in a reference initialization. Not sure I got my terminology right...
正如其他人所提到的,在实现中引用和指针基本上是相同的。 有一些小警告:
您不能将 NULL 分配给引用
(shoosh提到了这一点):那就是
很重要,因为没有
“未定义”或“无效”引用
值。
您可以通过临时
变量作为 const 引用,
但传递指针是不合法的
到一个临时的。
例如,这是可以的:
但大多数编译器会抱怨
同样,__restrict 关键字 不能应用于引用,仅应用于指针。
你不能用引用进行指针算术,所以如果你有一个指向数组的指针,那么数组中的下一个元素可以通过 p+1 获得,而引用在其整个生命周期中只指向一个事物.
As everyone else has mentioned, in implementation references and pointers are largely the same. There are some minor caveats:
You can't assign NULL to a reference
(shoosh mentioned this): that's
significant since there is no
"undefined" or "invalid" reference
value.
You can pass a temporary
variable as a const reference,
but it's not legal to pass a pointer
to a temporary.
For example, this is okay:
but most compilers will complain about
Similarly, the __restrict keyword cannot be applied to references, only pointers.
You can't do pointer arithmetic with references, so whereas if you have a pointer into an array then the next element in the array can be had through p+1, a reference only ever points at one thing in its entire life.
这些功能显然不“相同”,但就虚拟行为而言,它们的行为类似。 关于切片,只有当您处理值而不是引用或指针时才会发生这种情况。
The functions are obviously not "the same", but with regard to virtual behaviour they will behave similarly. Regarding slicing, this only happens when you deal withvalues, not references or pointers.