And explicit initialization function

Initialization list

1, initialization narrowly
defined variables directly when such behavior is called narrow initialization initialization:

int a = 3;

2. General initialized
first assigned to a variable called generalized initialization condition is called initialization

int a;
...//跟a无关的代码
a = 3

Corresponds to a narrow list initialization initialization, the internal structure corresponds to the generalized function initializes

So we can solve some of variable initialization list can only be initialized with a narrow initialization, for example:

  1. const variables
  2. Quote
  3. No no object class configuration parameters

3, explicit: to prevent non-standard single-argument constructor call

Single-argument constructor may be called directly by =, for example:
Suppose CT class constructors a single parameter or parameters of type int int related types, then, "CT a = 3;" This wording is to be allowed of.
But such an approach is very awkward, it looks like directly assigned to the 3 a, in order to avoid such an approach, you can add in the constructor explicit, so that such wording becomes invalid.

Published 77 original articles · won praise 23 · views 7556

Guess you like

Origin blog.csdn.net/Hots3y/article/details/100590499