I am confused with followed concepts:
string str=\"123\";
Some books say that: using \"=\" is copy initialization,
but some articles sa
Yes that is called copy initialization.
Instead of default constructing str and then constructing another string
from "123"
using string(const char*)
and then assigning the two strings, the compiler just construct a string using string(const char*) with "123".
string str="123"
is same asstring str("123")
. There is no doubt str("123") is directly initial
However remember that is possible only if the corresponding constructor is not explicit.
It's simply a matter of grammar:
T x = y;
is copy-initialization, and
T x(y);
is direct-initialization.
This is true for any type T
. What happens exactly depends on what sort of type T
is. For primitive types (e.g. int
s), the two are exactly the same. For class-types (such as std::string
), the two are practically the same, though copy-initialization requires that a copy-constructor be accessible and non-explicit (though it will not actually be called in practice).