Code
1 Int Array [] = { 1 , 2 , 3 , 4 , 5 };
2 # Define Total_elements (sizeof (array)/sizeof (array [0])
3
4 # Include < Iostream >
5 Using Namespace STD;
6
7 Class Base {
8 Public :
9 Explicit Base ( Int I = 0 ): Ival (I)
10 {
11 Cout < " Class base's constructure is called " < Endl;
12 }
13 Base ( Const Base & B)
14 {
15 Cout < " Class base's copy constructure is called " < Endl;
16 }
17 Public :
18 Int Ival;
19 };
20
21 Void Fun (Base B)
22 {
23 Cout < " Fun is called " < Endl;
24 }
25
26 Base fn1 ()
27 {
28 Base B;
29 Return B;
30 }
31
32 Base FN2 ()
33 {
34 Return Base ();
35 }
36
37 Void Main ()
38 {
39 Int Val = 5 ;
40 Fun (base (VAL ));
41 Base myb;
42 Fun (myb );
43 Cout < Endl;
44
45 Int D = - 1 ;
46 If (D < (Unsigned Char ) 1 )
47 Cout < " INT-1 <unsigned char 1 " < Endl;
48 If (D < (Unsigned Int ) 1 )
49 Cout < " INT-1 <unsigned int 1 " < Endl;
50
51 If (D <= Total_elements)
52 Cout < " INT-1 <total_elements " < Endl;
53
54 Cout < Endl;
55
56 Fn1 (); cout < Endl;
57 FN2 (); cout < Endl;
58
59 Base B1 = Fn1 (); cout < Endl;
60 Base B2 = FN2 (); cout < Endl;
61
62 Getchar ();
63 }
Running result:
Note that for the 40 rows, an unnamed object is used as the real parameter to call the function fun. However, when the compiler uses the real parameter to initialize the form parameter, it does not call the copy constructor like a common object to call fun. The initialization of the unknown object, that is, the initialization of the form parameter.
This technique can also be used for return statements of functions. For example, return student ("Lucy"); or student ST ("Lucy"); Return st; the former is better than the latter. (For example, call fn1 and FN2 functions)
For the latter, the St object is created, and the constructor is called to complete initialization. Then, the copy constructor is called to copy the st object to the external storage unit that saves the returned value. Finally, the St object is destroyed at the end of the function. For the former, the compiler can directly create and initialize temporary objects in external storage units, saving the overhead of copying and destructor.
Comparison Description: if an expression has different types of operands, a conversion occurs. The data type is generally converted to a value with higher floating point precision and longer memory usage. When int-1 and unsigned char 1 are compared, the latter is implicitly converted to 1 of the int type. Therefore, if the former is smaller than the latter, the relationship is established.
When the ratio of int-1 to unsigned int 1 is called the size, the former is converted to unsigned int, but-1 is converted to unsigned int to a very large value. Therefore, the relationship between the former and the latter is not valid. This interpretation also applies to the total_elements sentence, where sizeof returns an unsigned integer.