C ++ 11 introduced many new syntaxes that simplify programming. For now, we can call it "syntax dessert ". The following is a one-to-one introduction.
Syntax dessert 1: sequence for Loop
A sequence for loop is a simplified for loop that can be used to traverse a set of sequences, including containers, strings, arrays, initialization lists, and sequences defined by the begin and end functions. ExampleCodeAs follows:
1 Vector < Int > Vcttemp { 1 , 2 , 3 };
2 For (Auto A: vcttemp)
3 {
4 Cout <A <Endl;
5 }
Syntax dessert 2: Delegate constructor
Before C ++ 11 is introduced, if a class has multiple overloaded constructors and these constructors have some common initialization logic, you usually need to write another initialization function with parameters, and then call this initialization function in these constructors. In C ++ 11, you don't have to worry about it any more. We can implement a most basic constructor. All other constructor calls this constructor. The sample code is as follows:
1 Class Cperson
2 {
3 Public :
4 Cperson (): cperson ( 0 , "" ) {NULL ;}
5 Cperson ( Int Nage): cperson (Nage, "" ) {NULL ;}
6 Cperson ( Int Nage, Const String & Strname)
7 {
8 Stringstream SS;
9 SS <strname < " Is " <Nage < " Years Old. " ;
10 M_strinfo = ss. STR ();
11 }
12
13 Private :
14 String M_strinfo;
15 };
Syntax dessert 3: Unified initialization syntax
There are various initialization syntaxes before C ++ 11 is introduced. In C ++ 11, you can still use these initialization syntaxes, but you can also choose to use the new unified initialization syntax. The unified initialization syntax is represented by a pair of braces {}. The {} initialization syntax can effectively avoid narrow conversions. The sample code is as follows:
1 Int A { 5 };
2 Char C { ' X ' };
3 Int P [ 5 ] = { 1 , 2 , 3 , 4 , 5 };
4 Vector < Int > Vcttemp { 1 , 2 , 3 };
5 Cperson person { 10 , " Mike " };
6 Int B = 5.3 ; // B is assigned to 5, and a narrow conversion occurs.
7 Int D { 5.3 }; // A compilation error is prompted to avoid narrow conversions.
Syntax dessert 4: nullptr
Nullptr is a newly added keyword in C ++ 11, used to identify a null pointer. After nullptr is introduced, the ambiguity of some function overloading can be solved. The sample code is as follows:
1 Void F ( Int A)
2 {
3 Cout <A <Endl;
4 }
5
6 Void F ( Char * P)
7 {
8 Assert (P! = NULL );
9
10 Cout <p <Endl;
11 }
12
13 Int Main ( Int Argc, _ tchar * argv [])
14 {
15 Int * P = nullptr;
16 Int * Q = NULL;
17 Bool Bequal = (P = Q ); // The two pointer values are equal, and bequal is true.
18 Int A = nullptr; // Compilation failed. nullptr is not converted to int
19
20 F ( 0 ); // Compilation fails in C ++ 98, which has two meanings. In C ++ 11, F (INT) is called)
21 F (nullptr ); // Call F (char *)
22
23 Getchar ();
24 Return 0 ;
25 }
Syntax dessert 5: Initialize member variables
Similar to Java and C #, You can initialize member variables locally. The sample code is as follows:
1 Class Cperson
2 {
3 Private :
4 Int M_nage = 10 ;
5 String M_strname = " Mike " ;
6 };
Syntax dessert 6: Default function or disabled Function
When we define our own constructor with parameters, the compiler will no longer generate default constructor. If we want to use the default constructor at this time, you must explicitly declare and define constructors without parameters. In C ++ 11, we can use the default keyword to indicate that we want to use the default constructor. Similarly, when we do not want to use the constructors or value assignment functions automatically generated by the compiler, we generally need to declare them as protected or private. In C ++ 11, we can use the delete keyword to indicate that we do not want the compiler to generate the default constructor or value assignment function. The sample code is as follows:
1 Class Cperson
2 {
3 Public :
4 Cperson () = Default ;
5 Cperson ( Const Cperson & person) = Delete;
6 };
Syntax dessert 7: inherited Constructor
When a function in a derived class hides a function of the same name in the base class, if you want to export the function of the same name in the base class in the derived class, you can use using base :: func introduces the function of the same name in the base class to the scope of the derived class. This method is only valid for common member functions and cannot be used for constructors. In C ++ 11, if the derived class thinks that the base class constructor is sufficient, you can also use using base :: base to introduce the base class constructor to the scope of the derived class. Note that the member variables in the derived class are not initialized at this time, so these member variables should be initialized locally. The sample code is as follows:
1 Class Cbase
2 {
3 };
4
5 Class Cderived: Public Cbase
6 {
7 Public :
8 Using Cbase: cbase;
9 Cderived ( Int Ndata): m_ndata (ndata) {NULL ;}
10
11 Private :
12 Int M_ndata = 10 ;
13 };
Syntax dessert 8: Double parentheses on the right of the template
In C ++ 98, vector <int> vcttemp is an invalid expression, and the compiler considers it to be a shift operator, therefore, you must change it to vector <int> vcttemp, that is, add a space between the two on the right. In C ++ 11, this is no longer a problem. the compiler will be able to identify that the double parentheses on the right end of the two template parameter lists.
Syntax dessert 9: static_assert
Static assertions static_assert consist of a constant expression and a string. During compilation, the value of the constant expression will be calculated. If it is false, the string will be output as an error message. The sample code is as follows:
1 Char A = 10 ;
2 Static_assert ( Sizeof (A) = 4 , " A is not an integer. " );
Syntax dessert 10: initialization list
Before C ++ 11 is introduced, only Arrays can use the initialization list. In C ++ 11, various containers, such as vector and list, and strings can all use the initialization list. The class corresponding to the initialization list is initializer_list, vector, list and other containers, and the reason why string can use the initialization list is that they reload the constructor whose parameter type is initializer_list (called the initialization list constructor) and the value assignment function (called the initialization list assignment function ). The following are examples of using the initialization list.
1 Void Print ( Const Initializer_list < Int > & Ildata)
2 {
3 For (Auto A: ildata)
4 {
5 Cout <A <Endl;
6 }
7 }
8
9 Int Main ( Int Argc, _ tchar * argv [])
10 {
11 Vector < Int > Vctnum = { 1 , 2 , 3 , 4 , 5 };
12 Map < String , String > Mapid2name = {{ " 92001 " , " Jack " },{ " 92002 " , " Mike " }};
13 String Strtext { " Hello World " };
14 Print ({});
15 Print ({ 1 , 2 });
16 Print ({ 1 , 2 , 3 , 4 , 5 });
17
18 Getchar ();
19 Return 0 ;
20 }