2
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.

1
  • 1
    It runs overload resolution on all of the converting constructors of the destination type std::string (and any non-explicit conversion functions of the source type, if it's a class type). Commented Feb 23, 2016 at 4:16

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.