草庐IT

Unique_ptr

全部标签

c++ - 返回 shared_ptr<> 的成员函数的 const 正确性

如果我有课classfoo{private:std::shared_ptrm_data;public:std::shared_ptrGetData(){returnm_data;}}我相信std::shared_ptr应该转换为std::shared_ptr并共享同一个对象,但函数的const正确性是什么?IE这是有效的吗?std::shared_ptrGetData()const; 最佳答案 函数会修改对象的内部状态吗?没有。*函数是否允许其调用者(或其他外部环境)修改访问对象的内部状态?没有。这意味着将其标记为const是安全的

c++ - `weak_ptr::expired` 对象 dtor 中的行为

考虑以下代码:#include#includeusingnamespacestd;classT;std::weak_ptrwptr;classT{public:T(){}~T(){std::cout();wptr=ptr;std::cout在这段代码中,我试图找出weak_ptr是否在对象销毁阶段过期。似乎是这样。输出是:notexpiredindtorexpired我使用gcc-5.1和ideone.现在,我有另一个问题。我找不到任何说明这是标准行为的文档。是否保证以这种方式工作,总是? 最佳答案 Now,Ihaveanother

c++ - `weak_ptr::expired` 对象 dtor 中的行为

考虑以下代码:#include#includeusingnamespacestd;classT;std::weak_ptrwptr;classT{public:T(){}~T(){std::cout();wptr=ptr;std::cout在这段代码中,我试图找出weak_ptr是否在对象销毁阶段过期。似乎是这样。输出是:notexpiredindtorexpired我使用gcc-5.1和ideone.现在,我有另一个问题。我找不到任何说明这是标准行为的文档。是否保证以这种方式工作,总是? 最佳答案 Now,Ihaveanother

c++ - 如何将 shared_ptr 与指向不应释放的结构的指针一起使用

目前我正在使用glib库中的一些函数。伴随着glib的还有gio。glib是一个C库,因此我需要删除我创建的一些结构。我为许多对象创建了一个智能指针,例如:std::shared_ptrmy_queue=std::shared_ptr(g_async_queue_create(),g_async_queue_unref);为此创建了一个指向GAsyncQueue的共享指针,这会在队列生命结束时安全地销毁队列。但是,当我从gio库中获得一个我不应该释放的指针时,我遇到了一个问题。在下面的代码中,my_connection是一个GSocketClient,它实现(在glib中)GIOStr

c++ - 如何将 shared_ptr 与指向不应释放的结构的指针一起使用

目前我正在使用glib库中的一些函数。伴随着glib的还有gio。glib是一个C库,因此我需要删除我创建的一些结构。我为许多对象创建了一个智能指针,例如:std::shared_ptrmy_queue=std::shared_ptr(g_async_queue_create(),g_async_queue_unref);为此创建了一个指向GAsyncQueue的共享指针,这会在队列生命结束时安全地销毁队列。但是,当我从gio库中获得一个我不应该释放的指针时,我遇到了一个问题。在下面的代码中,my_connection是一个GSocketClient,它实现(在glib中)GIOStr

c++ - "cannot access private member' "只有当类有导出链接时才会出错

我最近不得不更改几个类的链接规范并遇到了问题。其中两个类包含一个std::map和一个std::unique_ptr作为值类型。更改链接后,编译器开始提示“无法访问在类'std::unique_ptr'中声明的私有(private)成员”错误。有谁知道为什么只有在提供导出规范或有解决方案时才会发生这种情况?示例代码:#includestructSomeInterface{virtual~SomeInterface()=0;};//ThisclasscompileswithnoproblemsstructLocalClass{std::map>mData;};//Thisclassfai

c++ - "cannot access private member' "只有当类有导出链接时才会出错

我最近不得不更改几个类的链接规范并遇到了问题。其中两个类包含一个std::map和一个std::unique_ptr作为值类型。更改链接后,编译器开始提示“无法访问在类'std::unique_ptr'中声明的私有(private)成员”错误。有谁知道为什么只有在提供导出规范或有解决方案时才会发生这种情况?示例代码:#includestructSomeInterface{virtual~SomeInterface()=0;};//ThisclasscompileswithnoproblemsstructLocalClass{std::map>mData;};//Thisclassfai

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

classMyClass{public:MyClass(std::weak_ptrparent){}}我想这样做:autonewInstance=std::make_shared(nullptr);或者weak_ptr参数的默认值为null,如:voidfunction(intarg,std::weak_ptrobj=nullptr);但是,我需要这样做:autonewInstance=std::make_shared(std::shared_ptr(nullptr));这是为什么呢? 最佳答案 因为weak_ptr在概念上只能从另

c++ - 为什么我不能将 nullptr 转换为 weak_ptr<>

classMyClass{public:MyClass(std::weak_ptrparent){}}我想这样做:autonewInstance=std::make_shared(nullptr);或者weak_ptr参数的默认值为null,如:voidfunction(intarg,std::weak_ptrobj=nullptr);但是,我需要这样做:autonewInstance=std::make_shared(std::shared_ptr(nullptr));这是为什么呢? 最佳答案 因为weak_ptr在概念上只能从另

c++ - clang 3.1 看不到 unique_ptr?

我刚刚开始玩clang并尝试编译以下示例程序:#include#includeintmain(){std::unique_ptru(newunsigned(10));std::cout编译时出现以下错误:$clang++helloworld.cpphelloworld.cpp:6:10:error:nomembernamed'unique_ptr'innamespace'std'std::unique_ptru(newunsigned(10));~~~~~^helloworld.cpp:6:29:error:expected'('forfunction-stylecastortypec