通过指针*或通过参考&? [英] Pass by Pointer * or Pass by Reference &?
问题描述
大家好,
我是一名学习C ++的C程序员。而且我对Pointer *或Pass by Reference&的函数Pass
感到困惑。
函数通过指针传递:
void func(int * x)
函数通过引用传递:
void func(int& x)
什么是他们的主要区别,并在什么情况下使用?
祝你好运,
罗伯特
Hello all,
I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.
Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)
What''s their main difference and be used in what circumstance?
Best regards,
Robert
推荐答案
" Robert" < ZH ******* @ gmail.com> schrieb im Newsbeitrag
新闻:11 ********************* @ g14g2000cwa.googlegro ups.com ...
"Robert" <zh*******@gmail.com> schrieb im Newsbeitrag
news:11*********************@g14g2000cwa.googlegro ups.com...
大家好,
我是一名学习C ++的C程序员。而且我对Pointer *或Pass by Reference&的功能传递感到困惑。
函数通过指针传递:
void func(int * x)
并且函数通过引用传递:
void func(int& x)
它们的主要区别是什么?在什么情况下使用?
最好的问候,罗伯特
Hello all,
I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.
Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)
What''s their main difference and be used in what circumstance?
Best regards,
Robert
指针可以为空,引用不可以。指针可以重新分配。
参考使用起来更舒适。你可以像普通的
对象一样处理它。更改函数时从
void func(const AClass);
到
void func(const AClass&);
你不需要重写你的代码。
问候Chris
Pointer can be null, references can''t. Pointers can be reassigned.
A reference is more comfortable to use. You can handle it like a normal
object. When changing a function from
void func(const AClass);
to
void func(const AClass&);
you do not need to rewrite your code.
Greetings Chris
Robert写道:
大家好,
我是一名学习C ++的C程序员。而且我对Pointer *或Pass by Reference&的功能传递感到困惑。
函数通过指针传递:
void func(int * x)
并且函数通过引用传递:
void func(int& x)
它们的主要区别是什么?在什么情况下使用?
最好的问候,罗伯特
Hello all,
I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.
Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)
What''s their main difference and be used in what circumstance?
Best regards,
Robert
在这本方便的指南中有很多关于此的信息:
http://www.parashift.com/c++-faq-lite/
- John Ratliff
There is a lot of good information about this in this handy guide:
http://www.parashift.com/c++-faq-lite/
--John Ratliff
另一个问题是为什么引用无法表示NULL
对象?
祝你好运,
罗伯特
Another problem is why reference cannot be able to represent a NULL
object?
Best regards,
Robert
这篇关于通过指针*或通过参考&?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!