New Features C ++ 2.0 (b) - <consistency initialization, Initializer_list, for circulation, explicit>

First, the consistency initialization (uniform initialization)

  Initialized before there is more than one version, allowing users to compare the confusing use, is now offering Wan initialization method used, is the use of braces.

  Analytical principle: When the compiler sees braces wrap things, it generates a initializer_list <T> (initializer_list it is in fact associated with an array <T, n>), and then call the constructor, the array one by one from the decomposition taken out and then call the constructor, but if the function itself provides initializer_list <T> parameter type of constructor, it will not break down but pass directly in the past.

 

// Initial Value Column: Forced initialized to 0 (or nullptr a).
Int I; I // initialized to an undefined value.
Int {J}; // J is initialized to 0 (braces can be used to set the initial value)
int * p; // p is initialized to an undefined value.
int * Q {}; // Q is initialized to 0 (braces can be used to set the initial value)

// narrowed (reduced accuracy or cause changes in value) in terms of the braces is not established.
Int X0 (3.4); // OK.
Int X1 = 3.4; // OK.
Int X2 {3.4}; // Wrong. (narrowing does not allow data processing, in fact, I only gave compiler warning)
int x3 = {3.4}; // Wrong. (narrowing does not allow data processing, in fact, I only gave compiler warning)
std :: the Vector < int> {V1. 1, 2,. 3}; // OK.
STD :: Vector <int> V2 {1.1, 2.2, 3.3}; // Wrong. (narrowing allowed data processing, in fact, only to compiler I caveat)

二、Initializer_list

Note: As long as the compiler encounters braces there are some number, re-transmission of values, he goes to generate a initializer_list <T> to deal with this template and variable parameters mentioned earlier compared to a chapter, this type must be consistent , while the latter can be freely combined type

Three, for circulation

Note: 1, figure above stated type of reference speed much faster, because the reference pointer corresponding to an operation operating only 4 bytes, rather than with reference to the speed of the overhead data type footprint growth increases, and to declare a reference the modified will directly affect the value of the data set.

      2, when used for container operations, the standard library provides associative containers are not allowed to modify the value of the modified container through the iterator that is declared as a reference for the cycle can not modify the values ​​associated with the container above

Four, explicit

  there has been explicit keywords only role in the constructor, the purpose is to prevent the compiler should not allow the constructor implicit conversion (that is not to try to be smart compiler), declared as explicit constructor can not be hidden conversion, only allows the user to explicitly call the constructor;

  In C ++ 2.0, explicit constructor can support more than one parameter (previously passed an argument to support only)

1,2.0 previously, explicit act on only a constructor argument

. 1 #include <the iostream>
 2  class Single {
 . 3  
. 4  public :
 . 5      // Common constructors (single argument) 
. 6      Single ( int A, B int = 0): NUM (A)
 . 7      {}
 . 8  Private :
 . 9      int NUM ;
 10  };
 . 11  
12 is  class SingleMore {
 13 is  public :
 14      // Explicit display affirms constructor (single argument) 
15      Explicit SingleMore ( int A): NUM (A)
 16      {}
 . 17  Private :
18 is      int NUM;
 . 19  };
 20 is  
21 is  
22 is  #if . 1
 23 is  int main ( int argc, char * the argv [])
 24  {
 25      Single SINGLE ( . 3 );
 26 is      Single Single2 = . 4 ;
 27  
28      SingleMore singleMore ( . 3 );
 29      singleMore2 = SingleMore . 4 ; // compile error, E0415 absence of transition from "int" to "SingleMore" appropriate constructor
 30  
31 is      return  0 ;
 32  }
 33 is  #endif

After 2,2.0 explicit arguments can be applied to a plurality of constructors

 

 

Guess you like

Origin www.cnblogs.com/laiyingpeng/p/11310266.html