string s = "abc";
The above statement will first invoke string ( const char * s )
constructor, then invoke copy constructor according to What are the differences in string initialization in C++? . Here is the question: how C++ know it should invoke string ( const char * s )
to convert literal string "abc" to a temporary string object?
Note : copy constructor won't be invoked in copy initialization.
Initializing an object by using the syntax
string s = "abc";
is called copy initialization .
There are several scenarios where that is legal initialization. In all cases the RHS must be convertible to a string
for it to work.
One way a string literal can be converted to a string
is through the constructor of string
that takes a char const*
. That is called a user defined conversion .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.