在QT2.3.10的源代码中看到如下使用三元运算符的方法,但不知道具体的运算的顺序
const QCanvasItem* i1 = s1 ?
(const QCanvasItem*)s1 : p1 ?
(const QCanvasItem*)p1 : r1 ?
(const QCanvasItem*)r1 : e1 ?
(const QCanvasItem*)e1 : (const QCanvasItem*)t1;
const QCanvasItem* i2 = s2 ?
(const QCanvasItem*)s2 : p2 ?
(const QCanvasItem*)p2 : r2 ?
(const QCanvasItem*)r2 : e2 ?
(const QCanvasItem*)e2 : (const QCanvasItem*)t2;
写了2个小程序分别测试Java和C++的三元运算符的运算顺序 从左到右
Java版本 class test String s = s1!=null ?printf(s1) : s2!=null ? printf(s2) : s3!=null ? printf(s3) : printf(s4); static String printf(String s){ 程序输出结果: s3 |
C++版本 #include <iostream> using namespace std;
int main(int argc,int argv[]){ string* s1 = 0; string* s = s1 ?printf(s1) : s2 ? printf(s2) : s3 ? printf(s3) : printf(s4); 输出结果: s2 s-->s2 |