“if语句中的条件表达式可以是任何算术表达式”中的算术表达式指的是基本类型组成的表达式么?

申祷无 2013-07-03 07:02:34
书上的原话:
这也意味着任何算术或者指针表达式都可以用做条件。
还是说算术表达式本来就是应该由基本类型组成的?

但是我写的这段代码报错了:
#include <iostream>

using namespace std;

class cond
{
int i;
public:
cond( int );
cond& operator +=( cond a ) //顺便问一下这个怎么像构造函数一样定义到类定义的外面。
{
i += a.i;
return *this;
}
};
cond::cond( int a )
{
i = a;
}

cond operator +( cond a, cond b )
{
cond c = a;
return c += b;
}

int main()
{
cond test1( 3 );
cond test2( 4 );

if( test1 + test2 ); //这里报错。

return 0;
}


我知道这个应该定义"operator bool();"。我只是想知道那句话中的算术表达式具体指什么。
...全文
2005 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
lin5161678 2013-07-08
  • 打赏
  • 举报
回复
只关注 0 和 非0 无法做到 0 和 非0 的 就给Error 完毕
lm_whales 2013-07-08
  • 打赏
  • 举报
回复
++,--也可以
lm_whales 2013-07-08
  • 打赏
  • 举报
回复
算术表达式意味着结果是内置类型的表达式 一元运算 +,- 二元运算 +,-,*,/,% 甚至赋值运算: += ,-=*=,/= %= = 好像位运算也可以: & ,|, ^ ,~ << >> 位运算和赋值运算的复合赋值运算: &=,|=,^=, <<=,>>= 逻辑运算,当然可以了; &&,||,! 关系运算的结果,是true,或者false(1,0) 当然也可以。 > ,>= ,==,<,<=
bluewanderer 2013-07-04
  • 打赏
  • 举报
回复
引用 8 楼 zhao4zhong1 的回复:
算术表达式就是最终能算出一个立即数的表达式。
退一万步说那也只能叫常数表达式
easy_fefrry 2013-07-04
  • 打赏
  • 举报
回复
看似简单,其实也知道深究。学习了!
赵4老师 2013-07-04
  • 打赏
  • 举报
回复
算术表达式就是最终能算出一个立即数的表达式。
unituniverse2 2013-07-04
  • 打赏
  • 举报
回复
引用 楼主 shendaowu 的回复:
书上的原话: 这也意味着任何算术或者指针表达式都可以用做条件。 还是说算术表达式本来就是应该由基本类型组成的? 但是我写的这段代码报错了:
#include <iostream>

using namespace std;

class cond
{
    int i;
public:
    cond( int );
    cond& operator +=( cond a ) //顺便问一下这个怎么像构造函数一样定义到类定义的外面。
    {
        i += a.i;
        return *this;
    }
};
cond::cond( int a )
{
    i = a;
}

cond operator +( cond a, cond b )
{
    cond c = a;
    return c += b;
}

int main()
{
   cond test1( 3 );
   cond test2( 4 );
   
   if( test1 + test2 ); //这里报错。
   
   return 0;
}
我知道这个应该定义"operator bool();"。我只是想知道那句话中的算术表达式具体指什么。
指的是一个 运算结果是bool类型或者可以隐士转换成bool类型的表达式。
申祷无 2013-07-04
  • 打赏
  • 举报
回复
引用 17 楼 rocktyt2 的回复:
标准中的说法是if的条件不管是单纯的表达式还是变量声明,只要值能转换为bool就可以 不管是不是什么算术,指针,自定义类型
The value of a condition that is an initialized declaration in a statement other than aswitchstatement is the value of the declared variable contextually converted tobool(Clause4). If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in aswitchstatement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. The value of a condition that is an expression is the value of the expression, contextually converted toboolfor statements other than switch; if that conversion is ill-formed, the program is ill-formed. The value of the condition will be referred to as simply “the condition” where the usage is unambiguous. 这个?
rocktyt 2013-07-04
  • 打赏
  • 举报
回复
翻了下原文,的确是算术表达式,不过下面有举例说明这句话的意思 按我的理解,这里的算术表达式是指计算结果的值是算术类型的表达式,对于算术类型和指针类型,可以直接通过!=0来转换为bool,所以才有这句话
ForestDB 2013-07-04
  • 打赏
  • 举报
回复
expression的定义应该看这里,http://msdn.microsoft.com/en-us/library/88d2h9zy.aspx。
ForestDB 2013-07-04
  • 打赏
  • 举报
回复
先看这里,http://msdn.microsoft.com/en-us/library/fh88ctk1.aspx,这里是if Statement的定义。 再看这里,http://msdn.microsoft.com/en-us/library/40b07dd5.aspx,这是expression的定义。
rocktyt 2013-07-04
  • 打赏
  • 举报
回复
标准中的说法是if的条件不管是单纯的表达式还是变量声明,只要值能转换为bool就可以 不管是不是什么算术,指针,自定义类型
申祷无 2013-07-04
  • 打赏
  • 举报
回复
引用 15 楼 rocktyt2 的回复:
哪本书上写的? 标准里可没有“算术表达式”这种概念
C++之父的C++程序设计语言。裘宗燕翻译的,这个译者经常把有通用翻译的词翻译成其他的东西,所以有可能是翻译的问题。
rocktyt 2013-07-04
  • 打赏
  • 举报
回复
哪本书上写的? 标准里可没有“算术表达式”这种概念
申祷无 2013-07-04
  • 打赏
  • 举报
回复
引用 13 楼 ForestDB 的回复:
那只是举例子啊 等价变换都可以 int foo() { return 1; } foo() + 2也可以
如果你想举例子的话,请把所有可能的形式都举出来,不想全举出来的话就进行描述。
ForestDB 2013-07-04
  • 打赏
  • 举报
回复
那只是举例子啊 等价变换都可以 int foo() { return 1; } foo() + 2也可以
申祷无 2013-07-04
  • 打赏
  • 举报
回复
引用 11 楼 ForestDB 的回复:
[quote=引用 10 楼 shendaowu 的回复:] [quote=引用 9 楼 ForestDB 的回复:] 1 + 2这样的。
#include <iostream>

using namespace std;

int main()
{
   int a = 1;
   int b = 2;
   
   while( a + b );
   
   return 0;
}
[/quote] 所以有什么问题么? [/quote] 你用的不是常量么?我的意思是变量也可以。
ForestDB 2013-07-04
  • 打赏
  • 举报
回复
引用 10 楼 shendaowu 的回复:
[quote=引用 9 楼 ForestDB 的回复:] 1 + 2这样的。
#include <iostream>

using namespace std;

int main()
{
   int a = 1;
   int b = 2;
   
   while( a + b );
   
   return 0;
}
[/quote] 所以有什么问题么?
申祷无 2013-07-04
  • 打赏
  • 举报
回复
引用 9 楼 ForestDB 的回复:
1 + 2这样的。
#include <iostream>

using namespace std;

int main()
{
   int a = 1;
   int b = 2;
   
   while( a + b );
   
   return 0;
}
ForestDB 2013-07-04
  • 打赏
  • 举报
回复
1 + 2这样的。
加载更多回复(7)

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧