說明: 該運算符不僅能用在變量之間,竟然還可以用在函數之間,相當於可傳參數的函數調用
1 void show_1(int val)
2 {
3 cout << "function show_1 called! and var is "<< var << endl;
4 }
5
6
7 void show_2(int val)
8 {
9 cout << "function show_2 called! and var is " << var << endl;
10 }
11
12
13 void quest_test()
14 {
15 int c = 0;
16 int a = 1, b = 2;
17 (c++ ? a : b)--;
18 cout << a << " and " << b << endl;
19 (c ? a : b)++;
20 cout << a << " and " << b << endl;
21 (c ? show_1 : show_2)(100);
22 }
23
24
25 最後的輸出是
26 1 and 1
27 2 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);
另類函數調用,可作參考