shared-ptr

shared_ptr with vector

只愿长相守 提交于 2019-12-01 11:41:45
问题 I currently have vectors such as: vector<MyClass*> MyVector; and I access using MyVector[i]->MyClass_Function(); I would like to make use of shared_ptr . Does this mean all I have to do is change my vector to: typedef shared_ptr<MyClass*> safe_myclass vector<safe_myclass> and I can continue using the rest of my code as it was before? 回答1: vector<shared_ptr<MyClass>> MyVector; should be OK. But if the instances of MyClass are not shared outside the vector, and you use a modern C++11 compiler,

How to pass shared_ptr to class with lower lifetime?

混江龙づ霸主 提交于 2019-12-01 08:47:57
I'd like to optimize my code. I have one class that have shared_ptr data member. In some method of this class I create objects that need use this member (just to get information from object pointed by shared_ptr). I know that lifetime of these created objects is lower than my main class. How to pass this pointer? I think another shared_ptrs are unnecessary (because I have warranty that object will exist). So what should get my created classes? Should they get raw pointer? Weak_ptr? Or the best solution is getting shared_ptr (and incrementing its reference counter)? What is the most standard

Using unique_ptr instead of shared_ptr in BST

浪尽此生 提交于 2019-12-01 08:33:19
I am trying to implement BST with unique_ptr . I got a working program for shared_ptr . How do I go about using unique_ptr instead to enforce the single ownership semantics of the BinarySearchTree? When I replace shared_ptr with unique_ptr , I get compilation errors beyond my understanding. #include <iostream> #include <memory> template<class T> class BinarySearchTree{ struct TreeNode; typedef std::shared_ptr<TreeNode> spTreeNode; struct TreeNode{ T data; spTreeNode left; spTreeNode right; TreeNode(const T & value):data(value),left(nullptr),right(nullptr){} }; spTreeNode root; bool insert

C++/CLI Wrapping a Function that Returns a std::shared_ptr

匆匆过客 提交于 2019-12-01 08:23:48
I'm currently wrapping a C++ class with C++/CLI for .NET interoperability following the standard process of holding a native pointer in a managed class. In one instance, I have a native class that has a function like: std::shared_ptr<BaseChannel> channelData(const int RunNumber); I have already begun creating a wrapper class for BaseChannel . However, if I pass the raw pointer to the constructor of the managed class, there are no guarantees on the lifetime of the object being pointed to by the managed class. I.e. the shared_ptr could go out of scope and the object will get deleted and the

C++: Replace raw pointers with shared and weak ptr

烈酒焚心 提交于 2019-12-01 07:03:26
I'm facing a design issue in my program. I have to manage Nodes object which are part of a root ChainDescriptor. Basically it looks like the following: class ChainDescriptor { public: ~ChainDescriptor() { //delete the nodes in nodes... } void addNode(Node *); Node * getNode(); const std::list<Node *>& getNodes() const; std::list<Node *> m_nodes; }; class Node { public: Node(Node *parent); void addChild(Node *node); Node * getChild(const std::string& nodeName); private: Node * m_parent; std::list<Node*> m_childs; }; The ChainDescriptor class owns all the nodes and is responsible of deleting

no type named 'shared_ptr' in namespace 'std'

核能气质少年 提交于 2019-12-01 06:49:48
Hopefully this helps somebody else Trying to compile an iOS project with Cedar BDD kept failing with no type named 'shared_ptr' in namespace 'std' error message. It was obviously a C++ error but couldn't understand why the C++ library Xcode was using didn;t have the shared_ptr type defined until I discovered the build settings of the target provides two libraries to choose from libstdc++ (compiler default) libc++ selecting libc++ fixes the problem see screenshot Apple ship a very old version of libstdc++ that doesn't support C++11, so if you use libstdc++ you can't use C++11 features. jeff In

Problems with shared_ptr<T[]> wrapping a dynamic array

孤街浪徒 提交于 2019-12-01 06:19:31
I wanted to replace some raw pointers in my class with a std::shared_ptr so that I don't have to worry when I create copies of that class. But the raw pointers point to a dynamic array. Using a shared_ptr with dynamic arrays is possible when you give it a custom deleter, e. g. default_delete<T[]> . But I get a big error list as soon as I try to assign a new value to that field, even on construction. Here's a minimal code sample: #include <memory> #include <cstddef> using namespace std; template<typename T> shared_ptr<T[]> make_shared_array(size_t size) { return shared_ptr<T[]>(new T[size],

Using unique_ptr instead of shared_ptr in BST

纵然是瞬间 提交于 2019-12-01 05:34:40
问题 I am trying to implement BST with unique_ptr . I got a working program for shared_ptr . How do I go about using unique_ptr instead to enforce the single ownership semantics of the BinarySearchTree? When I replace shared_ptr with unique_ptr , I get compilation errors beyond my understanding. #include <iostream> #include <memory> template<class T> class BinarySearchTree{ struct TreeNode; typedef std::shared_ptr<TreeNode> spTreeNode; struct TreeNode{ T data; spTreeNode left; spTreeNode right;

no type named 'shared_ptr' in namespace 'std'

北城余情 提交于 2019-12-01 05:22:21
问题 Hopefully this helps somebody else Trying to compile an iOS project with Cedar BDD kept failing with no type named 'shared_ptr' in namespace 'std' error message. It was obviously a C++ error but couldn't understand why the C++ library Xcode was using didn;t have the shared_ptr type defined until I discovered the build settings of the target provides two libraries to choose from libstdc++ (compiler default) libc++ 回答1: selecting libc++ fixes the problem see screenshot 回答2: Apple ship a very

Set shared_ptr to point existing object

那年仲夏 提交于 2019-12-01 04:43:50
For the code below, I would like to know how to set std::shared_ptr to point the given objects in the two member functions. The Vector3 object which is allocated in the main function is not going to be deleted until the end of the program. #include <memory> #include <vector> using std::vector; using std::shared_ptr; class Vector3 { // ... }; class Face { vector < shared_ptr <Vector3> > vtx; public: void addVtx (const Vector3& vt) { // vtx.push_back (); <-- how to push_back ? } void setVtx (const int v, const Vector3& vt) { if ( vtx.size() > 0 && v < vtx.size() ) { // vtx[v] = &vt; <-- how to