shared-ptr

How can I take a single element out of a boost library (e.g. shared_pointer)?

≯℡__Kan透↙ 提交于 2019-12-04 04:11:04
I've been playing around with some Boost components, and the only one I see a direct need for in the project I'm working on is boost::shared_ptr . Would it be difficult to just include the required files for shared_ptr , or at least just include files for the Boost smart_ptr directory in my project? They seem to have some external dependencies on other parts of Boost - but I figure there's an easy way to just use certain components of the Boost library and I'm missing it. If you can tell me what parts I need or point me to a good tutorial I'd be most grateful! Praetorian You can use the bcp

C++: Creating a shared object rather than a shared pointer to an object

半腔热情 提交于 2019-12-04 03:44:01
问题 boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but I wish that I could use the shared_ptr<A> as an A* . Consider the following code class A { public: A() {} A(int x) {mX = x;} virtual void setX(int x) {mX = x;} virtual int getX() const {return mX;} private: int mX; }; class HelpfulContainer { public: //Don't worry, I'll manager the memory from here. void eventHorizon(A*& a) { cout << "It's too late to save it now!" << endl; delete a; a = NULL; } };

How to access target of std::tr1::shared_ptr in GDB

百般思念 提交于 2019-12-04 03:26:34
How can I access target of a std::tr1::shared_ptr in GDB. This doesn't work: (gdb) p sharedPtr->variableOfTarget If I try with the pointer object itself ( p sharedPtr ) I get something like this: $1 = std::tr1::shared_ptr (count 2) 0x13c2060 With a normal pointer I can do p *ptr and get all the data or p ptr->variable for just one variable. I'm on Centos 6.5, GCC 4.4.7-4.el6 and GDB 7.2-64.el6_5.2. Try with (gdb) p (*sharedPtr.get()) that function returns the a pointer to the object owned by the smart pointer. ptr->get() not always work. when i try ptr->get(), gdb complains for: can not

Weak pointer to this in constructor

风格不统一 提交于 2019-12-04 03:17:20
I understand it is not possible to obtain a shared_ptr by calling shared_from_this() from the constructor of a class, as the object is not yet constructed. Is it possible however to obtain a weak_ptr to the object from the constructor? Some boost forum posts discussing a "weak_from_raw()" method suggests that it may be possible. Edit: Boost form discussing weak_from_raw http://lists.boost.org/boost-users/2010/08/61541.php I think what you're referencing is this . Which seems not to have been merged to the boost release (may be wrong about that). From the boost docs : Frequently Asked Questions

shared_ptr in std::tr1

一个人想着一个人 提交于 2019-12-04 02:49:40
I am working on a platform with a gcc compiler however boost cannot compile on it. I am wondering what is the proper way to include the shared_ptr in std:tr1 on gcc? the file i looked in said not to include it directly, from what i can tell no other file includes it either :| bdonlan In G++ 4.3 , #include <tr1/memory> should do the trick. You'll find shared_ptr at std::tr1::shared_ptr . Boost itself has the answer . Boost can not compile on it? Most of the boost library doesn't need to be compiled to be used, and I guess shared_ptr doesn't either. 来源: https://stackoverflow.com/questions/471438

why i can't cast nullptr to weak_ptr<>

半世苍凉 提交于 2019-12-04 02:47:14
问题 class MyClass { public: MyClass(std::weak_ptr<MyClass> parent){} } i want to do this: auto newInstance = std::make_shared<MyClass>(nullptr); or default value of weak_ptr argument is null, such as : void function(int arg,std::weak_ptr<MyClass> obj = nullptr); but, what i need is to do this instead: auto newInstance = std::make_shared<MyClass>(std::shared_ptr<MyClass>(nullptr)); why is that? 回答1: Because a weak_ptr in concept can only be constructed from another weak_ptr or shared_ptr . It just

Passing a shared pointer by reference or by value as parameter to a class

為{幸葍}努か 提交于 2019-12-04 02:14:24
Should a shared pointer be passed by reference or by value as a parameter to a class if it is going to be copied to a member variable? The copying of the shared pointer will increment the refrence count and I don't want to make any unnecessary copies and thus ref count increments. Will passing the shared pointer as a refrence solve this? I assume it does but are there any other problems with this? Passing by value: class Boo { public: Boo() { } }; class Foo { public: Foo(std::shared_ptr<Boo> boo) : m_Boo(boo) {} private: std::shared_ptr<Boo> m_Boo; }; int main() { std::shared_ptr<Boo> boo =

Overhead and implementation of using shared_ptr

 ̄綄美尐妖づ 提交于 2019-12-04 00:15:28
Short introduction: I am working on multithread code and I have to share dynamically allocated objects between two threads. To make my code cleaner (and less error-prone) I want to explicitly "delete" objects in each thread and that's why I want to use shared_ptr . First question: I want to know if implementation of -> operator in shared_ptr has some extra overhead (e.g. larger then unique_ptr ) during run time. Objects I am talking about are usually longlife instances copied only once after creation (when i distribute them between threads), then I only access these objects' methods and fields

C++: auto_ptr + forward declaration?

依然范特西╮ 提交于 2019-12-04 00:02:51
问题 I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor delete s it. This is working pretty well. Now I want to change this code to use auto_ptr so I write: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: std::auto_ptr<Inner> m_inner; }; Now, the constructor initialized the auto_ptr and the destructor does nothing. But it doesn't

Differences between tr1::shared_ptr and boost::shared_ptr?

女生的网名这么多〃 提交于 2019-12-03 23:57:19
问题 Are there any difference between tr1::shared_ptr and boost::shared_ptr ? If so, what? 回答1: No, the documentation of boost shared_ptr says: This implementation conforms to the TR1 specification, with the only exception that it resides in namespace boost instead of std::tr1. 来源: https://stackoverflow.com/questions/3831572/differences-between-tr1shared-ptr-and-boostshared-ptr