好像以前讨论过^^,奇怪的问题。

Kevin_qing 2005-09-14 05:51:47
不多说,看代码
#include "stdio.h"

class Int{
public:
int operator =(int i)
{
return n=i;
}
Int(int i):n(i){}
Int operator ++(int){
printf("++");
return n++;
}
Int operator %(int i){
printf("%");
return n%i;
}
operator int(){
printf("int");
return n;
}
protected:
int n;
};

int main(){
Int i=2;
i=(i++)%3;
printf("%d\n",static_cast<int>(i));
return 0;
}

请问i是Int时输出?
i为内建的int时输出?

...全文
205 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tailzhou 2005-09-15
  • 打赏
  • 举报
回复
我的理解
i=(i++)%3;
内建的int时;
等价于:i=i%3;i++ 结果为3;

为Int时:首先计算(i++),重载函数返回++前的值2,但++操作在函数执行完毕的时候执行了,此时的i=3;
然后i又被2%3覆盖,所以为2;
Kevin_qing 2005-09-15
  • 打赏
  • 举报
回复
继续,,intel c++9.0
小写int结果是2。
Kevin_qing 2005-09-15
  • 打赏
  • 举报
回复
i是小写int,谁算出来是2的?
Jagen在路上 2005-09-15
  • 打赏
  • 举报
回复
我就是在这两个环境下测试的阿!都输出2
i是不可能等于3的啊。
Kevin_qing 2005-09-15
  • 打赏
  • 举报
回复
不用管操作符里的printf.
实际结果VC/GCC下
Int时输出2。
int时输出3。

谁给个解释
Joevan 2005-09-14
  • 打赏
  • 举报
回复
程序原意的话
Int operator %(int i)
{
printf("%%");//这个地方确实要改的哦
return n%i;
}
最好把这个也改一下:
operator int(){
printf("Int");//改成大写比较好,呵
return n;
}

程序没有错误,结果为++%int2

Int i=2;//你是这样定义的,所以肯定是i是Int时输出
每操作i一步,都会引发一个printf的啊
悠云guo 2005-09-14
  • 打赏
  • 举报
回复
另:
要输出百分号的话,应该改为
Int operator %(int i)
{
printf("%%");
return n%i;
}

悠云guo 2005-09-14
  • 打赏
  • 举报
回复
Int operator ++(int)
{
printf("++");
return ++n;
}
如果改为这样,就是输出"++%int0"

(gdb) b 37
Breakpoint 1 at 0x4012f2: file E:/helly/cprogram/TESTINTX.CPP, line 37.
(gdb) r
Starting program: E:\helly\cprogram/TESTINTX.exe

Breakpoint 1, main () at E:/helly/cprogram/TESTINTX.CPP:37
37 Int i = 2;
(gdb) s
Int::Int(int) (this=0x22ff8c, i=2) at E:/helly/cprogram/TESTINTX.CPP:11
11 Int( int i ) : n( i ) {}
(gdb)
main () at E:/helly/cprogram/TESTINTX.CPP:38
38 i = ( i++ ) % 3;
(gdb)
Int::operator++(int) (this=0x22ff8c) at E:/helly/cprogram/TESTINTX.CPP:15
15 printf( "++" );
(gdb)
16 return ++n;//这样的话,就是先自增再传入Int()构造函数
(gdb)
Int::Int(int) (this=0x22ff44, i=3) at E:/helly/cprogram/TESTINTX.CPP:11
11 Int( int i ) : n( i ) {}
(gdb)
Int::operator++(int) (this=0x22ff8c) at E:/helly/cprogram/TESTINTX.CPP:17
17 }
(gdb)
Int::operator%(int) (this=0x22ff84, i=3) at E:/helly/cprogram/TESTINTX.CPP:21
21 printf( "%%" );
(gdb)
22 return n % i;
(gdb)
Int::Int(int) (this=0x22ff54, i=0) at E:/helly/cprogram/TESTINTX.CPP:11
11 Int( int i ) : n( i ) {}
(gdb)
Int::operator%(int) (this=0x22ff84, i=3) at E:/helly/cprogram/TESTINTX.CPP:23
23 }
(gdb)
main () at E:/helly/cprogram/TESTINTX.CPP:39
39 printf( "%d\n", static_cast<int>( i ) );
(gdb)
Int::operator int() (this=0x22ff8c) at E:/helly/cprogram/TESTINTX.CPP:27
27 printf( "int" );
(gdb)
28 return n;
(gdb)
29 }
(gdb)
main () at E:/helly/cprogram/TESTINTX.CPP:40
40 return 0;
(gdb)
41 }
(gdb)
0x0040122d in __mingw_CRTStartup ()
(gdb)
Single stepping until exit from function __mingw_CRTStartup,
which has no line number information.

Program exited normally.
悠云guo 2005-09-14
  • 打赏
  • 举报
回复
(gdb) b 37
Breakpoint 1 at 0x4012f2: file E:/helly/cprogram/TESTINT.CPP, line 37.
(gdb) r
Starting program: E:\helly\cprogram/TESTINT.exe

Breakpoint 1, main () at E:/helly/cprogram/TESTINT.CPP:37
37 Int i = 2;
(gdb) s
Int::Int(int) (this=0x22ff8c, i=2) at E:/helly/cprogram/TESTINT.CPP:11
11 Int( int i ) : n( i ) {}
(gdb)
main () at E:/helly/cprogram/TESTINT.CPP:38
38 i = ( i++ ) % 3;
(gdb)
Int::operator++(int) (this=0x22ff8c) at E:/helly/cprogram/TESTINT.CPP:15
15 printf( "++" );
(gdb)
16 return n++;//关键是这里,先传n到Int的构造函数,再自增
(gdb)
Int::Int(int) (this=0x22ff44, i=2) at E:/helly/cprogram/TESTINT.CPP:11
11 Int( int i ) : n( i ) {}
(gdb)
Int::operator++(int) (this=0x22ff8c) at E:/helly/cprogram/TESTINT.CPP:17
17 }
(gdb)
Int::operator%(int) (this=0x22ff84, i=3) at E:/helly/cprogram/TESTINT.CPP:21
21 printf( "%%" );
(gdb)
22 return n % i;
(gdb)
Int::Int(int) (this=0x22ff54, i=2) at E:/helly/cprogram/TESTINT.CPP:11
11 Int( int i ) : n( i ) {}
(gdb)
Int::operator%(int) (this=0x22ff84, i=3) at E:/helly/cprogram/TESTINT.CPP:23
23 }
(gdb)
main () at E:/helly/cprogram/TESTINT.CPP:39
39 printf( "%d\n", static_cast<int>( i ) );
(gdb)
Int::operator int() (this=0x22ff8c) at E:/helly/cprogram/TESTINT.CPP:27
27 printf( "int" );
(gdb)
28 return n;
(gdb)
29 }
(gdb)
main () at E:/helly/cprogram/TESTINT.CPP:40
40 return 0;
(gdb)
41 }
(gdb)
0x0040122d in __mingw_CRTStartup ()
(gdb)
Single stepping until exit from function __mingw_CRTStartup,
which has no line number information.

Program exited normally.
snowbirdfly 2005-09-14
  • 打赏
  • 举报
回复
不是啊~~
结果是:++int2
heixuejun 2005-09-14
  • 打赏
  • 举报
回复
就是 没有什么问题阿 答案就是2
Jagen在路上 2005-09-14
  • 打赏
  • 举报
回复
都输出2阿,有错吗?

64,649

社区成员

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

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