#include <iostream>
#include <string>
using namespace std;
void func(int i)
{
cout << "func() : i = " << i << endl;
}
int main()
{
//(0,1,2):结果是2
//所以数组就赋值了前三个元素:2,5,8
//其他都初始化为0
//把()改成{ },就是正常的了
int a[3][3] = {
(0, 1, 2),
(3, 4, 5),
(6, 7, 8)
};
int i = 0;
int j = 0;
while( i < 5 )
func(i),
i++;
//以上逗号操作符,等同于:
/*while(i<5)
{
fun(i);
i++;
}
*/
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout << a[i][j] << endl;
}
}
(i, j) = 6;
//等价为j=6;
cout << "i = " << i << endl;
cout << "j = " << j << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int i)
{
mValue = i;
}
int value()
{
return mValue;
}
};
Test& operator , (const Test& a, const Test& b)
{
return const_cast<Test&>(b);
}
Test func(Test& i)
{
cout << "func() : i = " << i.value() << endl;
return i;
}
int main()
{
Test t0(0);
Test t1(1);
Test tt = (func(t0), func(t1)); // Test tt = func(t1);
cout << tt.value() << endl; // 1
return 0;
}
等价:
若将逗号操作符重载注释掉,那么打印的是:
先执行func(t0),然后执行func(t1)
逗号操作符最好不要去重载