这些都一样吗:
int foo(bar* p) {返回 p->someInt();}和
int foo(bar& r) {返回 r.someInt();}忽略空指针的潜力.无论 someInt() 是虚拟的还是传递一个 bar 或 bar 的子类,这两个函数在功能上是否相同?>
这个切片有什么作用:
bar&ref = *ptr_to_bar;C++ 引用在标准中有意未指定使用指针实现.引用更像是变量的同义词",而不是指向它的指针.当有可能意识到指针在某些情况下是多余的时,这种语义为编译器打开了一些可能的优化.
还有一些区别:
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;
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:
这篇关于指针和引用参数之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
编译器如何处理编译时分支?What do compilers do with compile-time branching?(编译器如何处理编译时分支?)
我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?)
在 C/C++ 中检查空指针Checking for NULL pointer in C/C++(在 C/C++ 中检查空指针)
比较运算符的数学式链接-如“if((5<j<=1))&quMath-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比较运算符的数学式链接-如“if((5<j<=1)))
“if constexpr()"之间的区别与“if()"Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之间的区别与“if())
C++,'if' 表达式中的变量声明C++, variable declaration in #39;if#39; expression(C++,if 表达式中的变量声明)