Strange behavior of copy-initialization, doesn't call the copy-constructor!

前端 未结 6 720
时光说笑
时光说笑 2021-01-02 13:15

I was reading the difference between direct-initialization and copy-initialization (§8.5/12):

T x(a);  //direct-initialization
T y = a; //copy-initialization         


        
6条回答
  •  萌比男神i
    2021-01-02 13:46

    This is an optimization by the compiler.

    In evaluating: A a = 10; instead of:

    1. first constructing a temporary object through A(int);

    2. constructing a through the copy constructor and passing in the temporary;

    the compiler will simply construct a using A(int).

提交回复
热议问题