参考vs指针..? [英] reference vs pointer .. ?
问题描述
问题是关于这两个对象与对象指针的引用之间的差异
任何一个都有什么优势何时使用它们。当一个对象作为参数传递给函数时,我看到很多例子
它作为参考传递。虽然把它作为一个指针也可以通过
一样好吗?是吗?
我在C lang新闻组中发布了一个类似的问题,但没有得到很多帮助,因为C没有引用对象。
2004年6月30日21:06:34 -0700,m sergei< se ******** @ yahoo.com>写道:
问题是关于这两个对象的区别对象与指向对象的指针
有什么好处在任何一个和什么时候会使用它们。当一个对象作为参数传递给一个函数时,我看到了很多例子。
它作为参考传递。虽然将它作为指针传递它也没关系?是对的吗 ?
如果作为指针传递,它将起作用。但是
1)指针可以是NULL,所以函数必须测试它。
2)指针不能用a初始化暂时的,所以你输了
灵活性
X f();
void g(const X&);
g(f()); //罚款
X f();
void g(const X *);
g(& f() ); //编译错误
我在C lang新闻组中发布了一个类似的问题但由于C没有对对象的引用而没有得到很多帮助。
真的不足为奇。
john
> 1)指针可以是NULL,所以函数必须测试它。
2)指针不能用临时初始化,所以你失去了灵活性
和
3)指针可以被索引,而引用则不能。因此,通过使用
引用,您隐式声明这不是数组。
4)使用引用的语法与使用引用的语法相同对象在
堆栈中,因此更容易重写以前在堆栈上工作的代码
对象使用引用而不是使用指针。
Niels Dybdahl
2004年7月1日星期四10:23:06 +0200,Niels Dybdahl
< nd*@fjern.detteesko-graphics.com>写道:
和
3)指针可以索引,而引用不能。因此,通过使用
引用,您隐式声明这不是数组。
不一定。
对于内置类型的引用,这是事实。但是,用户定义的
类型可以重载operator [],你当然可以将引用
索引到任何类型的指针。事实上你有一个参考
并不会自动禁止从语法的
角度使用operator []。
-
Bob Hairgrove
否******** **@Home.com
Question is about the difference between these two
reference to an object versus pointer to an object
What are advantages in either one and when would one use them. I see
lots of examples when an object is passed as a parameter to a function
it is passed as a reference. Though it would have been fine to pass it
as a pointer as well ? is that right ?
I had posted a similar question in the C lang newsgroup but did not
get much help as C does not do references to objects.
On 30 Jun 2004 21:06:34 -0700, m sergei <se********@yahoo.com> wrote:
Question is about the difference between these two
reference to an object versus pointer to an object
What are advantages in either one and when would one use them. I see
lots of examples when an object is passed as a parameter to a function
it is passed as a reference. Though it would have been fine to pass it
as a pointer as well ? is that right ?
It would work if passed as a pointer. But
1) A pointer can be NULL, so the function must test for that.
2) A pointer cannot be initialised with a temporary, so you lose
flexibility
X f();
void g(const X&);
g(f()); // fine
X f();
void g(const X*);
g(&f()); // compile error
I had posted a similar question in the C lang newsgroup but did not
get much help as C does not do references to objects.
Not surprising really.
john
> 1) A pointer can be NULL, so the function must test for that.
2) A pointer cannot be initialised with a temporary, so you lose
flexibility
and
3) A pointer can be indexed while a reference can not. So by using a
reference you implicit state that this is not an array.
4) The syntax for using a reference is the same as for using an object on
the stack, so it is easier to rewrite code that previously worked on a stack
object to use a reference than to use a pointer.
Niels Dybdahl
On Thu, 1 Jul 2004 10:23:06 +0200, "Niels Dybdahl"
<nd*@fjern.detteesko-graphics.com> wrote:
and
3) A pointer can be indexed while a reference can not. So by using a
reference you implicit state that this is not an array.
Not necessarily.
For references to built-in types, this is true. However, user-defined
types can overload operator[], and you can of course index a reference
to a pointer of any type. The mere fact that you have a reference
doesn''t automatically prohibit the use of operator[] from the
standpoint of the grammar.
--
Bob Hairgrove
No**********@Home.com
这篇关于参考vs指针..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!