C语言中的三目运算符 ?: 的新用法

来源:互联网 发布:数组排序 js 方法几种 编辑:程序博客网 时间:2024/06/05 18:03

说明: 该运算符不仅能用在变量之间,竟然还可以用在函数之间,相当于可传参数的函数调用

 void show_1(int val)
 
{
     cout << "function show_1 called! and var is "<< var <<
 endl;
 
}
 

 
 void show_2(int
 val)
 
{
     cout << "function show_2 called! and var is " << var <<
 endl;
10 
}
11 

12 
13 void
 quest_test()
14 
{
15     int 0
;
16     int 1, 2
;
17     (c++ b)--
;
18     cout <<  << and  << <<
 endl;
19     (c b)++
;
20     cout <<  << and  << <<
 endl;
21     (c show_1 show_2)(100
);
22 
}
23 

24 
25 
最后的输出是
26 and 1

27 and 1
28 function show_1 called and var is 100!

 

其中:

   

(c ? show_1 : show_2)(100);

其实等同于

if (c)
  show_1(100);
else
  show_2(100);

 

另类函数调用,可作参考

0 0
原创粉丝点击