一个有意思的题目

noock 2008-04-12 05:20:09
请写出程序输出结果:

#include <iostream>
using std::ostream;
using std::cout;
using std::endl;

class Widget
{
private:
int value;

public :

Widget& operator=(const Widget& w)
{
value = w.value;
return *this;
}

Widget& operator=(const int i)
{
value = i;
return *this;
}

friend ostream& operator<<(ostream& o, const Widget& w);


static void test(void)
{
cout<<"Testing int:"<<endl;
testInt();
cout<<"Testing Widget:"<<endl;
testWidget();
}

private:
#define INIT(a,b,c) ((a)=1,(b)=2,(c)=3)
static void testInt(void)
{
int a,b,c;
INIT(a,b,c);
a=b=c=4;
cout<<"a=b=c=4 ==> a="<<a<<",b="<<b<<",c="<<c<<endl;
INIT(a,b,c);
a=(b=(c=4));
cout<<"a=(b=(c=4)) ==> a="<<a<<",b="<<b<<",c="<<c<<endl;
INIT(a,b,c);
((a=b)=c)=4;
cout<<"((a=b)=c)=4 ==> a="<<a<<",b="<<b<<",c="<<c<<endl;
}

static void testWidget(void)
{
Widget a,b,c;
INIT(a,b,c);
a=b=c=4;
cout<<"a=b=c=4 ==> a="<<a<<",b="<<b<<",c="<<c<<endl;
INIT(a,b,c);
a=(b=(c=4));
cout<<"a=(b=(c=4)) ==> a="<<a<<",b="<<b<<",c="<<c<<endl;
INIT(a,b,c);
((a=b)=c)=4;
cout<<"((a=b)=c)=4 ==> a="<<a<<",b="<<b<<",c="<<c<<endl;
}
};
ostream& operator<< (ostream& o, const Widget& w)
{
o<<w.value;
return o;
}
...全文
139 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tgh621 2008-04-16
  • 打赏
  • 举报
回复
运行一下就有结果了赛
morya 2008-04-16
  • 打赏
  • 举报
回复
语句不完整,没有main函数调用

64,646

社区成员

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

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