为什么我不使用参考 [英] why I don't use references

查看:55
本文介绍了为什么我不使用参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用的动机似乎很清楚:当人们真正想要的只是对对象的引用时,阻止人们使用讨厌的

指针。


但C ++引用是如此不足以至于我仍然使用指针来对我的

引用对象。我发现它们不足的原因有三个:


1 - 不能为空。

2 - 不能重新安排。


现在我确定这些前两个有充分的理由,但是我的b $ b不能超过:#3 br />

3 - 不知道哪些参数是引用...

如果我写一个fn sig为''void f(int x,int * y)'',然后客户代码是

''f(a,& b)'',所以每个人都清楚发生了什么。

但是,如果我写一个fn sig为''void f(int x,int& y)'',那么客户端代码

就是''f(a,b)'',并且编译器不会告诉编码器他已经误解了这个函数,并且有人在审查代码时没有注意到

f会改变''b''。 br />

有解决方案吗?


TIA,汤姆。

解决方案
"汤姆" <否**** @ NoSpam.com>在留言中写道

新闻:Q2 ****************** @ news20.bellglobal.com ...

引用的动机似乎很清楚:当人们真正想要的只是对对象的引用时,阻止人们使用讨厌的指针。

但C ++引用是如此不足以至于我仍在使用指针我的
引用对象。我发现它们不足的原因有三个:

1 - 不能为空。
2 - 不能重新安置。


这就是参考应该是什么。


现在我确定这些有充分的理由2,但是#3我不能克服:

3 - 不知道哪些参考文献是参考...
如果我写一个fn sig为''void f(int x,int * y)'',那么客户端代码就是'f(a,& b)'',所以每个人都清楚什么是继续。
但是,如果我写一个fn sig为''void f(int x,int& y)'',那么客户端
代码是''f(a,b)''并且编译器不会告诉编码器他已经误解了这个函数,并且有人审查代码时不会注意到
f改变''b''。



程序员应检查功能签名。否则他怎么知道

参数需要什么?


有解决方案吗?



是总是检查函数参数类型和返回类型。



-

Ioannis


*编程页面: http://www.noicys.freeurl.com

*备选网址1: http://run.to/noicys

*备选网址2: http://www.noicys .cjb.net


" Tom" <否**** @ NoSpam.com>写在...

引用的动机似乎很清楚:当他们真正想要的只是对象的引用时,阻止人们使用讨厌的指针。


那是胡说八道。如果指针没用,他们就会从语言中取出
。如果引用没用,

也没有它们的位置。

但是C ++引用是如此不足以至于我仍在使用指针为我的
对象的引用。 [...]


好​​吧,当一个人开始责备时,这绝对不是一个好兆头。

一个人对工具的不足之处已选择使用......

有没有解决方案呢?




也许是一门很好的C ++课程。


Victor


Tom< No **** @ NoSpam.com>在留言中写道

新闻:Q2 ****************** @ news20.bellglobal.com ...

引用的动机似乎很清楚:当人们真正想要的只是对对象的引用时,阻止人们使用讨厌的指针。

但C ++引用是如此不足以至于我仍在使用指针我的
引用对象。我发现它们不足的原因有三个:

1 - 不能为空。
2 - 不能重置。

现在我我确信这些前2个有充分的理由,但是我不能克服这个问题:

3 - 不能说出哪些参数是引用...
如果我写一个fn sig为''void f(int x,int * y)'',那么客户端代码是'(f,a,& b) '',所以每个人都清楚发生了什么。
但是,如果我写一个fn sig为''void f(int x,int& y)'',那么客户端
代码是'f(a,b)'',并且编译器不会告诉编码器他已经误解了这个函数,并且有人审查代码时不会注意到
f变化''b''。

是否有解决方案?




是的,如果你在这种情况下你根本就不能使用引用对他们来说是不愉快的。指针有其用途,参考文献有

使用。显然,如果你想要null或重新选择的选项,引用

是不合适的。如果一个函数通过函数参数改变某些东西,那么就像你已经解释过的那样,优先选择一个指针。但是

这个案子怎么样?


void f(const VeryLargeObject& obj);


任何理由这里更喜欢指针吗?


另外:

std :: cout<< 结果: <<结果<< std :: endl;


如果流没有从其运算符<<<?< >

DW


The motivation for references seems clear: stop people from using nasty
pointers when all they really want is a reference to an object.

But C++ references are so inadequate that I''m still using pointers for my
references to objects. There are 3 reasons why I find them inadequate:

1 - Can''t be null.
2 - Can''t be reseated.

Now I''m sure there are good reasons for these first 2, but it''s #3 that I
can''t get over:

3 - Can''t tell which parms are references...
If I write a fn sig as ''void f( int x, int* y )'', then the client code is
''f( a, &b )'', so it is clear to everyone what is going on.
BUT, if I write a fn sig as ''void f( int x, int& y )'', then the client code
is ''f( a, b )'', and the compiler won''t tell the coder that he has
misunderstood the function, and someone reviewing the code won''t notice that
f changes ''b''.

Is there a solution to this?

TIA, Tom.

解决方案

"Tom" <No****@NoSpam.com> wrote in message
news:Q2******************@news20.bellglobal.com...

The motivation for references seems clear: stop people from using nasty
pointers when all they really want is a reference to an object.

But C++ references are so inadequate that I''m still using pointers for my
references to objects. There are 3 reasons why I find them inadequate:

1 - Can''t be null.
2 - Can''t be reseated.
And that''s what a reference is supposed to be.

Now I''m sure there are good reasons for these first 2, but it''s #3 that I
can''t get over:

3 - Can''t tell which parms are references...
If I write a fn sig as ''void f( int x, int* y )'', then the client code is
''f( a, &b )'', so it is clear to everyone what is going on.
BUT, if I write a fn sig as ''void f( int x, int& y )'', then the client code is ''f( a, b )'', and the compiler won''t tell the coder that he has
misunderstood the function, and someone reviewing the code won''t notice that f changes ''b''.

A programmer should check the function signature. Else how can he know what
arguments it takes?


Is there a solution to this?


Yes always check the function argument types and return type.



--
Ioannis

* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net


"Tom" <No****@NoSpam.com> wrote in...

The motivation for references seems clear: stop people from using nasty
pointers when all they really want is a reference to an object.
That''s nonsense. If pointers were useless, they would have
been taken out of the language. If references were useless,
there would be no place for them, too.
But C++ references are so inadequate that I''m still using pointers for my
references to objects. [...]
Well, it''s definitely not a good sign when one begins blaming
one''s own inadequacies on the tools one has chosen to use...
Is there a solution to this?



A good C++ course, perhaps.

Victor


Tom <No****@NoSpam.com> wrote in message
news:Q2******************@news20.bellglobal.com...

The motivation for references seems clear: stop people from using nasty
pointers when all they really want is a reference to an object.

But C++ references are so inadequate that I''m still using pointers for my
references to objects. There are 3 reasons why I find them inadequate:

1 - Can''t be null.
2 - Can''t be reseated.

Now I''m sure there are good reasons for these first 2, but it''s #3 that I
can''t get over:

3 - Can''t tell which parms are references...
If I write a fn sig as ''void f( int x, int* y )'', then the client code is
''f( a, &b )'', so it is clear to everyone what is going on.
BUT, if I write a fn sig as ''void f( int x, int& y )'', then the client code is ''f( a, b )'', and the compiler won''t tell the coder that he has
misunderstood the function, and someone reviewing the code won''t notice that f changes ''b''.

Is there a solution to this?



Yes, you could simply not use references in such cases if you are
uncomfortable with them. Pointers have their uses and references have their
uses. Obviously, where you want the option of null or reseating, references
are not suitable. Where a function changes something via a function argument
there is, as you''ve explained, a good case for preferring a pointer. But
what about this case?

void f(const VeryLargeObject &obj);

Any reason to prefer a pointer here?

Also:
std::cout << "Result: " << result << std::endl;

How would this be done if a stream did not return a reference to itself from
its operator<<?

DW


这篇关于为什么我不使用参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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