vendredi 14 août 2020

pointer to const int using smart pointer

I am trying to create a smart pointer (unique_ptr) to a value returned as const int& but my issue can be summed up as easily as:

 const int value = 5;
 const int * ptr{nullptr};
 ptr = &value;

This works, and compile as expected. When trying to the same using smart pointer:

 const int value = 5;
 std::unique_ptr<const int> ptr{nullptr};
 ptr = &value;

With this I get the compile error:

no operator "=" matches these operands -- operand types are: std::unique_ptr<const int, std::default_delete<const int>> = const int *

Is it possible to get the same behaviour as normal C pointers?

Aucun commentaire:

Enregistrer un commentaire