Implicit VS Explicit Conversion

前端 未结 4 583
[愿得一人]
[愿得一人] 2020-11-30 23:19

The C++ Standard Library by Nicolai M. Josuttis states:

There is a minor difference between

X x;
Y y(x) //explicit conversion

and

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 23:40

    The first form is direct initialization. The second is copy initialization.

    Copy initialization implicitly calls a converting constructor or conversion operator and then explicitly calls a copy constructor (the copy constructor call may be elided, but an accessibility check must still be performed).

    Consider a third possibility, which is copy-initialization, but the conversion is explicit:

    Y y = Y(x);
    

    or

    Y y = (Y)x;
    

提交回复
热议问题