smart-pointers

What's the difference between BSTR and _bstr_t?

前提是你 提交于 2019-12-18 10:53:05
问题 Can anyone explain the difference between the types mentioned above and some sample usage to clearly explain the difference between the two? Any help would be highly appreciated! Note: this question is a spin-off from this other question 回答1: BSTR is the string data type used with COM. _bstr_t is a wrapper class that works like a smart pointer, so it will free the allocated memory when the variable is destroyed or goes out of scope. _bstr_t also has reference counting, which increases every

How to pass deleter to make_shared?

允我心安 提交于 2019-12-18 10:48:08
问题 Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use make_shared or make_unique to avoid some error prone. If we like to use a smart pointer class, like shared_ptr , we can construct one like, shared_ptr<int> p(new int(12)); Also we would like to pass a custom deleter to smart pointer classes, shared_ptr<int>

Clone pattern for std::shared_ptr in C++

若如初见. 提交于 2019-12-18 09:05:13
问题 Why do you need (in order to make it compile) the intermediate CloneImplementation and std::static_pointer_cast (see Section 3 below) to use the Clone pattern for std::shared_ptr instead of something closer (see Section 2 below) to the use of raw pointers (see Section 1 below)? Because as far as I understand, std::shared_ptr has a generalized copy constructor and a generalized assignment operator? 1. Clone pattern with raw pointers : #include <iostream> struct Base { virtual Base *Clone()

Typedef a shared_ptr type with a static custom deleter, similar to unique_ptr

独自空忆成欢 提交于 2019-12-18 06:57:48
问题 I have read through many questions on SO on custom deleter for shared_ptr and unique_ptr , and the difference between the two. But, I still haven't found any clear answer to this question: How can one best go about creating a type that acts as a shared_ptr with a custom deleter, similar to how unique_ptr has the deleter as part of the type definition? For unique_ptr usage, I use a deleter class, that handles deletion of individual types (limiting it to just two types, for brevity): struct SDL

C++11 Smart Pointer Policies

☆樱花仙子☆ 提交于 2019-12-17 23:33:56
问题 As I understand it, in the current specification of C++11, one should use: std::unique_ptr<> for one owner (most of the time) std::shared_ptr<> only when there are multiple owners in acyclic structure std::weak_ptr<> sparingly only when there are cycles that need to be broken A raw pointer as a handle to memory (no ownership) when a reference would not suffice So my questions are: Are these policies sufficient or are there additional policies that I should be aware of? Are scoped_ptr<> and

Boost weak_ptr's in a multi-threaded program to implement a resource pool

佐手、 提交于 2019-12-17 20:26:20
问题 I'm thinking of using boost::weak_ptr to implement a pool of objects such that they will get reaped when nobody is using one of the objects. My concern, though, is that it's a multi-threaded environment, and it seems there's a race condition between the last shared_ptr to an object going out of scope and a new shared_ptr being constructed from the weak_ptr. Normally, you'd protect such operations with lock or something; however, the whole point here is that you don't know when the shared_ptr

Create shared_ptr to stack object

∥☆過路亽.° 提交于 2019-12-17 19:35:13
问题 In my method a Player object is created like: Player player(fullName,age); My teacher gave us a piece of code with a constructor that takes a shared_ptr to a player object. //constructor of the class SomeClass(const std::shared_ptr<Socket> client, std::shared_ptr<Player> player) Lets say we want to call the constructor of SomeClass and pass the player object we created on stack. Is it ever safe/possible/good to create a shared_ptr from a stack object? To make the question more understandable

Using unique_ptr to control a file descriptor

与世无争的帅哥 提交于 2019-12-17 19:23:02
问题 In theory, I should be able to use a custom pointer type and deleter in order to have unique_ptr manage an object that is not a pointer. I tried the following code: #ifndef UNIQUE_FD_H #define UNIQUE_FD_H #include <memory> #include <unistd.h> struct unique_fd_deleter { typedef int pointer; // Internal type is a pointer void operator()( int fd ) { close(fd); } }; typedef std::unique_ptr<int, unique_fd_deleter> unique_fd; #endif // UNIQUE_FD_H This doesn't work (gcc 4.7 with the -std=c++11

intrusive_ptr in c++11

我的未来我决定 提交于 2019-12-17 18:13:19
问题 Does C++11 have something equivalent to boost::intrusive_ptr ? My problem is that I have a C-style interface over my C++ code. Both sides of the interface can use C++, but exposing the C interface is required for compatibility reasons. I cannot use std::shared_ptr because I have to manage the object through two (or more) smart pointers. I am unable to figure out a solution with something like boost::intrusive_ptr . 回答1: Does c++11 have something equivalent to boost::intrusive_ptr? No. It does

Bad practice to return unique_ptr for raw pointer like ownership semantics?

*爱你&永不变心* 提交于 2019-12-17 17:59:44
问题 I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a unique_ptr . class Foobar { public: static unique_ptr<Foobar> factory(DataObject data); } My intent is to tell client code that they own the pointer. Without a smart pointer, I would simply return Foobar* . I would like, however, to enforce that