平等操作员问题 [英] equality operator question

查看:57
本文介绍了平等操作员问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在通常使用条件语句如


if(0 == i)

if(-1 == i)

if(true == i)

if(5 == i)


比较传统的陈述,如


if(i == 0)

if(i == -1)

if(i == true)

如果(i == 5)


适用于所有类型的''我'(POD和类)?

Is it now common practice to use conditional statements like

if (0 == i)
if (-1 == i)
if (true == i)
if (5 == i)

over the more traditional statements like

if (i == 0)
if (i == -1)
if (i == true)
if (i == 5)

for all types of ''i'' (PODs and classes)?

推荐答案

On Sun,2008年5月4日22:40:28 -0400,barcaroller

< ba ******** *@music.netwrote in comp.lang.c ++:
On Sun, 4 May 2008 22:40:28 -0400, "barcaroller"
<ba*********@music.netwrote in comp.lang.c++:

现在通常的做法是使用条件语句,例如


if(0 == i)

if(-1 == i)

if(true == i)

if(5 = = i)


更传统的陈述,如


if(i == 0)

if( i == -1)

if(i == true)

if(i == 5)


适用于所有类型的''i'(POD和类)?
Is it now common practice to use conditional statements like

if (0 == i)
if (-1 == i)
if (true == i)
if (5 == i)

over the more traditional statements like

if (i == 0)
if (i == -1)
if (i == true)
if (i == 5)

for all types of ''i'' (PODs and classes)?



在相等比较中首先写常量的原因是

非常简单。一个相当常见的印刷错误只是一个类型只有一个

''=''偶尔有两个是有意的,需要的。


考虑一下不小心打字:


如果(i = 0)


....当你打算输入:


if(i == 0)


假设内置类型,或者甚至是具有作业的类类型

运算符接受int,那么第一个版本,类型,不仅仅是
总是假的,但它也会破坏''i'的现有值。然而

它在语法上是正确的并被编译器吞没而没有

投诉。


另一方面,如果你不小心输入如果(0 = i),编译器

将发出诊断并拒绝它。


所以这是一种非常便宜的方法来捕捉打字第一次编译时,错误就是

。这不是一个非常常见的错误,但是偶尔会发生在每个人身上的一个b / b
。当然,当你比较两个物体时,它并没有提供帮助,但是与

常数的比较相当频繁。


有些程序员认为它很丑陋并且拒绝使用它。

我认为它很有用并且我使用它。它可以工作,它可以节省时间,当你确实碰到这个错字时。


-

Jack Klein

主页: http://JK-Technology.Com
常见问题解答

comp.lang.c http:/ /c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.club.cc .cmu.edu / ~ajo / docs / FAQ-acllc.html

The reason for writing the constant first in an equality comparison is
quite simple. A fairly common typographical error is type only one
''='' once in a while when two are intended, and needed.

Consider the implications of accidentally typing:

if (i = 0)

....when you meant to type:

if (i == 0)

Assuming a built-in type, or even a class type having an assignment
operator accepting an int, then the first version, the type, is not
only always false, but it also destroys the existing value of ''i''. Yet
it is syntactically correct and swallowed by the compiler without
complaint.

On the other hand, if you accidentally type "if (0 = i), the compiler
will emit a diagnostic and reject it.

So it is a very inexpensive method of catching a typing error on the
very first compile. This is not an extremely common error, but is one
which happens to everyone once in a while. And of course it doesn''t
help when you are comparing two objects, but comparisons with a
constant are fairly frequent.

Some programmers consider it hideously ugly and refuse to ever use it.
I think it is useful and I use it. It works, and it saves time when
you do happen to make that typo.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


Jack Klein写道:
Jack Klein wrote:

On Sun,2008年5月4日22:40:28 -0400,barcaroller

< ba ********* @ music.netwrote in comp.lang .c ++:
On Sun, 4 May 2008 22:40:28 -0400, "barcaroller"
<ba*********@music.netwrote in comp.lang.c++:

>现在通常的做法是你条件语句如

if(0 == i)
if(-1 == i)
if(true == i)
if(5 = = i)

更传统的陈述,如

if(i == 0)
if(i == -1)
if( i == true)
如果(i == 5)

所有类型的''我'(POD和类)?
>Is it now common practice to use conditional statements like

if (0 == i)
if (-1 == i)
if (true == i)
if (5 == i)

over the more traditional statements like

if (i == 0)
if (i == -1)
if (i == true)
if (i == 5)

for all types of ''i'' (PODs and classes)?



在相等比较中首先写常量的原因是

非常简单。一个相当常见的印刷错误只是一个类型只有一个

''=''偶尔有两个是有意的,需要的。


考虑一下不小心打字:


如果(i = 0)


...当你打算打字时:


if(i == 0)


假设内置类型,或者甚至是具有赋值的类类型

运算符接受int ,那么第一个版本,类型,不仅仅是b $ b总是假的,但它也会破坏''i'的现有价值。然而

它在语法上是正确的并被编译器吞没而没有

投诉。


另一方面,如果你不小心输入如果(0 = i),编译器

将发出诊断并拒绝它。


所以这是一种非常便宜的方法来捕捉打字第一次编译时,错误就是

。这不是一个非常常见的错误,但是偶尔会发生在每个人身上的一个b / b
。当然,当你比较两个物体时,它并没有提供帮助,但是与

常数的比较相当频繁。


有些程序员认为它很丑陋并且拒绝使用它。

我认为它很有用并且我使用它。它可以工作,并且它可以节省时间,当你确实发生了这个错字。


The reason for writing the constant first in an equality comparison is
quite simple. A fairly common typographical error is type only one
''='' once in a while when two are intended, and needed.

Consider the implications of accidentally typing:

if (i = 0)

...when you meant to type:

if (i == 0)

Assuming a built-in type, or even a class type having an assignment
operator accepting an int, then the first version, the type, is not
only always false, but it also destroys the existing value of ''i''. Yet
it is syntactically correct and swallowed by the compiler without
complaint.

On the other hand, if you accidentally type "if (0 = i), the compiler
will emit a diagnostic and reject it.

So it is a very inexpensive method of catching a typing error on the
very first compile. This is not an extremely common error, but is one
which happens to everyone once in a while. And of course it doesn''t
help when you are comparing two objects, but comparisons with a
constant are fairly frequent.

Some programmers consider it hideously ugly and refuse to ever use it.
I think it is useful and I use it. It works, and it saves time when
you do happen to make that typo.



我同意,但是如果在

if语句中进行作业,我的编译器会发出警告。否则我可能会使用那种格式。但是因为它确实是b $ b,我坚持使用更传统的if(变量==值)


-

Jim Langston
ta*******@rocketmail.com




" Jim Langston" < ta ******* @ rocketmail.com写了留言

新闻:wM *************** @ newsfe05.lga ...

"Jim Langston" <ta*******@rocketmail.comwrote in message
news:wM***************@newsfe05.lga...

Jack Klein写道:
Jack Klein wrote:



< ...>

<...>


>在相等比较中首先写常量的原因很简单。一个相当普遍的印刷错误只是偶尔输入一个
''='',当有两个是有意的时候,需要它。

考虑意外打字的含义:

if(i = 0)

...当你打算输入:

如果(i == 0)

假设一个内置类型,甚至一个具有赋值操作符接受int的类类型,那么第一个版本,类型,不仅仅是总是假的,但它也会破坏现有的' '一世''。然而,它在语法上是正确的,并且在没有抱怨的情况下被编译器吞没。

另一方面,如果你不小心输入if(0 = i),编译器
将发出诊断并拒绝它。

因此,这是一种非常便宜的方法,可以在第一次编译时捕获输入错误。这不是一个非常常见的错误,但是偶尔会发生在每个人身上。当然,当你比较两个对象时它并没有帮助,但是与
常量的比较是相当频繁的。

一些程序员认为它很丑陋并且拒绝曾经使用过它。
我觉得它很有用,而且我用它。它可以工作,并且它可以节省您确实碰巧犯错的时间。
>The reason for writing the constant first in an equality comparison is
quite simple. A fairly common typographical error is type only one
''='' once in a while when two are intended, and needed.

Consider the implications of accidentally typing:

if (i = 0)

...when you meant to type:

if (i == 0)

Assuming a built-in type, or even a class type having an assignment
operator accepting an int, then the first version, the type, is not
only always false, but it also destroys the existing value of ''i''. Yet
it is syntactically correct and swallowed by the compiler without
complaint.

On the other hand, if you accidentally type "if (0 = i), the compiler
will emit a diagnostic and reject it.

So it is a very inexpensive method of catching a typing error on the
very first compile. This is not an extremely common error, but is one
which happens to everyone once in a while. And of course it doesn''t
help when you are comparing two objects, but comparisons with a
constant are fairly frequent.

Some programmers consider it hideously ugly and refuse to ever use it.
I think it is useful and I use it. It works, and it saves time when
you do happen to make that typo.



我同意,但是如果在

if语句中进行作业,我的编译器会发出警告。否则我可能会使用那种格式。但既然它是b $ b,我坚持使用更传统的if(变量==值)


I agree, but my compiler warns me if an assignment is being made inside an
if statement. Otherwise I probably would use that format. But since it
does, I stick with the more traditional if ( variable == value )



当然要有效地解决这个问题可以简单*替换*

违规操作员:


#define IS_EQUAL_TO ==


#define INITIALISE_WITH =


#define PLUS +


#define OUTPUT<<


#define CONTENTS_OF *


#define POINTER *


#define ADDRESS_OF&


#include< iostream>


int main()


{


int x INITIALISE_WITH 1;


int POINTER px INITIALISE_WITH ADDRESS_OF x;


if(CONTENTS_OF px PLUS 1 IS_EQUAL_TO 2){


std :: cout OUTPUT谁需要运营商?\ n;


}


}

问候


Andy Little



Of course to address the problem effectively one could simply *replace* the
offending operators:

#define IS_EQUAL_TO ==

#define INITIALISE_WITH =

#define PLUS +

#define OUTPUT <<

#define CONTENTS_OF *

#define POINTER *

#define ADDRESS_OF &

#include <iostream>

int main()

{

int x INITIALISE_WITH 1;

int POINTER px INITIALISE_WITH ADDRESS_OF x;

if ( CONTENTS_OF px PLUS 1 IS_EQUAL_TO 2){

std::cout OUTPUT "Who needs operators?\n";

}

}

regards

Andy Little



这篇关于平等操作员问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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