Confused with direct initialization and copy initialization

前端 未结 2 585
南笙
南笙 2021-01-22 00:20

I am confused with followed concepts:

string str=\"123\";

Some books say that: using \"=\" is copy initialization,

but some articles sa

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 01:21

    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. ints), 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).

提交回复
热议问题