C++ multiple unique pointers from same raw pointer

后端 未结 3 1706
无人及你
无人及你 2021-02-20 12:27

Consider my code below. My understanding of unique pointers was that only one unique pointer can be used to reference one variable or object. In my code I have more than one uni

3条回答
  •  暖寄归人
    2021-02-20 12:33

    This example of code is a bit artificial. unique_ptr is not usually initialized this way in real world code. Use std::make_unique or initialize unique_ptr without storing raw pointer in a variable:

    unique_ptr  uniquePtr2(new int);
    

提交回复
热议问题