引用vs指针 [英] reference vs pointer

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

问题描述



在我的一次采访中,我被问到一个问题,是否使用指针

参数是否有效,然后参考。


即虚无趣(复杂* p)

虚无趣(复杂&参考)


有人可以告诉我哪一个会是效率更高,为什么?


据我所知,两者必须相同,因为我在内部阅读了某些内部

引用被实现为常量指针

Hi,
In one of my interview I was asked a question, whether using pointers for
argument is efficient then reference or not.

i.e. void fun(Complex *p)
void fun(Complex &ref)

can somebody tell me which one will be more efficient and why?

as far as i know both must be same, because i read somewhere that internally
references are implemented as "constant pointers"

推荐答案

" Sandy" < a@a.com>在留言新闻中写道:de ******** @ netnews.net.lucent.com ...
"Sandy" <a@a.com> wrote in message news:de********@netnews.net.lucent.com...

在我的一次采访中,我被问到一个问题,是否使用
参数指针是有效的,然后参考或不参考。

即无效的乐趣(复杂* p)
无趣的乐趣(复杂& ref)

据我所知,两者必须相同,因为我读到内部
参考实施的地方作为常量指针
Hi,
In one of my interview I was asked a question, whether using pointers for
argument is efficient then reference or not.

i.e. void fun(Complex *p)
void fun(Complex &ref)

can somebody tell me which one will be more efficient and why?

as far as i know both must be same, because i read somewhere that
internally
references are implemented as "constant pointers"




性能(和底层实现)通常是相同的,

尽管优化编译器在某些情况下可能会生成更快的代码

的参考参数,因为它知道地址不是NULL。


那么什么时候应该使用任何一个选项来传递参数?

除非希望能够提供NULL作为参数,否则

引用应该是首选C ++程序中的红色。

但是,出于可读性的原因,我已经看到编码样式指南要求

使用指针输出变量:

processData(src1,src2,& dst); //突出''dst''作为输出

这不是我的首选方法。

希望这会有所帮助,

Ivan
-
http:// ivan。 vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++<> http://www.brainbench.com



Performance (and underlying implementation) is typically the same,
although an optimizing compiler might in some cases generate faster code
for the reference parameter, because it knows the address is not NULL.

So when should either option be used for passing parameters?
Unless it is desirable to be able to provide NULL as a parameter,
references ought to be preferred in C++ programs.
However, I have seen coding style guides that mandated the
use of pointers to output variables, for readability reasons:
processData( src1, src2, &dst ); // highlights ''dst'' as an output
This is not my preferred approach.
Hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



" Sandy" < a@a.com>在留言新闻中写道:de ******** @ netnews.net.lucent.com ...

"Sandy" <a@a.com> wrote in message news:de********@netnews.net.lucent.com...

在我的一次采访中,我被问到一个问题,是否使用
参数指针是有效的,然后参考或不参考。

即无效的乐趣(复杂* p)
无趣的乐趣(复杂& ref)
Hi,
In one of my interview I was asked a question, whether using pointers for
argument is efficient then reference or not.

i.e. void fun(Complex *p)
void fun(Complex &ref)

can somebody tell me which one will be more efficient and why?




除了伊万已经说过的,你可能会发现以下链接

帮助:
http:// www .parashift.com / c ++ - faq-lite / references.html


问候,

Sumit。

-

Sumit Rajan< su ********* @ gmail.com>



Apart from what Ivan has already said, you may find the following link
helpful:
http://www.parashift.com/c++-faq-lite/references.html

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>


有1个区别b / wa指针和引用。

指针可以指向NULL值。

当指针递增或递减时,它将指向下一个

相对于其基本类型的先前值。


引用不能指向NULL。

因为引用是一个隐式指针&对象的另一个名称

或变量,递增或递减参考将只对

进行操作。

对于独立引用,初始化必须完成。


参考或指针是否有效...取决于应用程序。

因为,我认为对于数据结构,只有指针才会更多

有用,比参考更多。


任何其他意见表示赞赏。


AV。


Sandy写道:
There is 1 difference b/w a pointer and a reference.
Pointer can point to a NULL value.
When a pointer is incremented or decremented, it will point to the next
or previous value relative to its base type.

Reference cannot point to NULL.
Since reference is an implicit pointer & a another name for an object
or variable, incrementing or decrementing the reference will just
operate on the value.
For independent references, an initialization has to be done.

Reference Or Pointer Is Efficient...Depends on the application.
Because, I think for data structures, only pointers will be more
useful, than references.

Any other opinion is appreciated.

AV.

Sandy wrote:

在我的一次采访中,我被问到一个问题,是否使用指针进行
参数是有效的,然后参考或者没有。

即无效的乐趣(复杂* p)
无趣的乐趣(复杂和参考)

有人可以告诉我哪一个会更有效率为什么呢?

据我所知,两者必须相同,因为我读到某处内部
引用实现为常量指针
Hi,
In one of my interview I was asked a question, whether using pointers for
argument is efficient then reference or not.

i.e. void fun(Complex *p)
void fun(Complex &ref)

can somebody tell me which one will be more efficient and why?

as far as i know both must be same, because i read somewhere that internally
references are implemented as "constant pointers"






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

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