使用智能指针指向 const int 的指针
pointer to const int using smart pointer
我正在尝试创建一个智能指针 (unique_ptr
),指向一个返回值 const int&
,但我的问题可以简单地概括为:
const int value = 5;
const int * ptr{nullptr};
ptr = &value;
这有效,并按预期编译。当尝试使用智能指针进行相同操作时:
const int value = 5;
std::unique_ptr<const int> ptr{nullptr};
ptr = &value;
有了这个我得到了编译错误:
no operator "=" matches these operands -- operand types are: std::unique_ptr<const int, std::default_delete<const int>> = const int *
是否可以获得与普通 C 指针相同的行为?
编辑:
我发现我原来的问题过于简单:这里是更高级的版本:
int value = 5;
const int& getValue(){
return value;
};
std::unique_ptr<const int> ptr1{nullptr};
const int * ptr2{nullptr};
ptr1 = std::make_unique<const int>(getValue());
ptr2 = &getValue();
std::cout << "ptr1: " << *ptr1 << "\n";
std::cout << "ptr2: " << *ptr2 << "\n";
value++;
std::cout << "ptr1: " << *ptr1 << "\n";
std::cout << "ptr2: " << *ptr2 << "\n";
打印出来:
ptr1: 5
ptr2: 5
ptr1: 5
ptr2: 6
如您所见,行为有点不同,现在我相信这是因为 make_unique
复制了指向内存地址中的值
std::unique_ptr
不能直接用裸指针赋值;你可以使用 reset
。但是你不应该分配 value
的地址(当自动离开范围时它会被销毁),std::unique_ptr
将尝试 delete
指向 UB 的指针。
你可能想要
int value = 5; // we'll constructor a new object, value doens't need to be const
std::unique_ptr<const int> ptr{nullptr};
ptr = std::make_unique<const int>(value); // construct a new object and pass the ownership to ptr
编辑
为了使用目的 smart pointers,
Smart pointers are used to make sure that an object is deleted if it is no longer used (referenced).
如果不想让智能指针管理对象,或者不能让智能指针拥有对象,就不要使用智能指针。对于这种特殊情况,我觉得用raw pointer就好了。
std::unique_ptr
是对原始指针和内存分配机制的包装。而且更多的是关于内存分配。它旨在自动创建和销毁对象。
这一行:
auto ptr = std::make_unique<const int>(5);
相当于:
auto ptr = new const int{5};
所以在你的行中
ptr1 = std::make_unique<const int>(getValue());
ptr1 指向一个 const int 类型的新对象,用 getValue() 返回的值初始化。
并且您不会在程序中更改此值。如果您这样尝试:
*ptr.get() += 1;
你会得到编译错误,因为 int 是 const。
我正在尝试创建一个智能指针 (unique_ptr
),指向一个返回值 const int&
,但我的问题可以简单地概括为:
const int value = 5;
const int * ptr{nullptr};
ptr = &value;
这有效,并按预期编译。当尝试使用智能指针进行相同操作时:
const int value = 5;
std::unique_ptr<const int> ptr{nullptr};
ptr = &value;
有了这个我得到了编译错误:
no operator "=" matches these operands -- operand types are: std::unique_ptr<const int, std::default_delete<const int>> = const int *
是否可以获得与普通 C 指针相同的行为?
编辑: 我发现我原来的问题过于简单:这里是更高级的版本:
int value = 5;
const int& getValue(){
return value;
};
std::unique_ptr<const int> ptr1{nullptr};
const int * ptr2{nullptr};
ptr1 = std::make_unique<const int>(getValue());
ptr2 = &getValue();
std::cout << "ptr1: " << *ptr1 << "\n";
std::cout << "ptr2: " << *ptr2 << "\n";
value++;
std::cout << "ptr1: " << *ptr1 << "\n";
std::cout << "ptr2: " << *ptr2 << "\n";
打印出来:
ptr1: 5
ptr2: 5
ptr1: 5
ptr2: 6
如您所见,行为有点不同,现在我相信这是因为 make_unique
复制了指向内存地址中的值
std::unique_ptr
不能直接用裸指针赋值;你可以使用 reset
。但是你不应该分配 value
的地址(当自动离开范围时它会被销毁),std::unique_ptr
将尝试 delete
指向 UB 的指针。
你可能想要
int value = 5; // we'll constructor a new object, value doens't need to be const
std::unique_ptr<const int> ptr{nullptr};
ptr = std::make_unique<const int>(value); // construct a new object and pass the ownership to ptr
编辑
为了使用目的 smart pointers,
Smart pointers are used to make sure that an object is deleted if it is no longer used (referenced).
如果不想让智能指针管理对象,或者不能让智能指针拥有对象,就不要使用智能指针。对于这种特殊情况,我觉得用raw pointer就好了。
std::unique_ptr
是对原始指针和内存分配机制的包装。而且更多的是关于内存分配。它旨在自动创建和销毁对象。
这一行:
auto ptr = std::make_unique<const int>(5);
相当于:
auto ptr = new const int{5};
所以在你的行中
ptr1 = std::make_unique<const int>(getValue());
ptr1 指向一个 const int 类型的新对象,用 getValue() 返回的值初始化。
并且您不会在程序中更改此值。如果您这样尝试:
*ptr.get() += 1;
你会得到编译错误,因为 int 是 const。