一个有意思的题目
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;
}