Direct initialization and copy initialization in c++

Direct initialization : It is to directly call the constructor of the class to initialize . as follows:

string a;//Call the default constructor

string a("hello");//Call the constructor whose parameters are of type const char*

string b(a);//Call the copy constructor  like string b = a;

 

Copy-initialization: Refers to initializing an object with an "=" sign  . as follows:

string a="hello";

string b = a;

The above two ways of writing are completely equivalent but in some cases they are still used differently.

 

Copy initialization: It should first call the corresponding constructor to create a temporary object , and then call the copy constructor to copy the temporary object to the object to be created.

As above: string a= "hello"; is the case above.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172712&siteId=291194637