smart-pointers

Dynamic Pointer Cast

家住魔仙堡 提交于 2020-01-13 06:07:27
问题 I'd like to convert a base class pointer to a derived class pointer as a function argument without using dynamic_pointer_cast class Base { public: typedef std::shared_ptr < Base > Ptr; virtual ~Base ( ); ... }; class Derive : public Base { public: typedef std::shared_ptr < Derive > Ptr; ... }; void foo( Derive::Ptr ptr ) { ... } Base::Ptr ptr1 ( new Derive ); foo( ptr1 ); The above code will give an error while calling foo. This can be avoided by typecasting ptr1 into a Dervie pointer using

Is it OK to inherit from the C++11 smart pointers and override the relative operators?

亡梦爱人 提交于 2020-01-12 12:53:05
问题 According to cppreference.com, std::shared_ptr provides a full set of relative operators (==, !=, <, ...), but the semantics of comparison aren't specified. I assume they compare the underlying raw pointers to the referenced objects, and that std::weak_ptr and std::unique_ptr do the same. For some purposes, I would prefer to have relative operators that order the smart pointers based on comparing the referenced objects (rather than the pointers to them). This is already something I do a lot,

Is there a C++/CLI smart pointer project (e.g. scoped_ptr)?

谁说胖子不能爱 提交于 2020-01-12 04:40:08
问题 Is there a C++/CLI RAII smart pointer class for containment of a native pointer in a managed type? Just wondering, before I go write my own clr_scoped_ptr value class template. I'm aware of the Microsoft-provided: containment of a managed handle in a native class: auto_gcroot containment of a managed handle in a managed class: auto_handle The above two are similar to auto_ptr or unique_ptr . I gave skeleton code for a counted_handle here, similar to shared_ptr But all these are for disposing

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

心已入冬 提交于 2020-01-11 15:49:26
问题 boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r , but .get() will return p . not r.get() ! This means you can do something like this: int main() { boost::shared_ptr<int> x(new int); boost::shared_ptr<int> y(x, new int); std::cout << x.get() << std::endl; std::cout << y.get() << std::endl; std::cout << x.use_count() << std::endl; std:

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

落花浮王杯 提交于 2020-01-11 15:49:25
问题 boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r , but .get() will return p . not r.get() ! This means you can do something like this: int main() { boost::shared_ptr<int> x(new int); boost::shared_ptr<int> y(x, new int); std::cout << x.get() << std::endl; std::cout << y.get() << std::endl; std::cout << x.use_count() << std::endl; std:

Design pattern to detect memory leaks for reference counted smart pointers

喜欢而已 提交于 2020-01-07 03:55:17
问题 We have our own smart pointers class which is reference counted using basic AddRef and Release. While debugging I am able to see lot of objects not being released properly. I can see which objects not being released but its hard to find out from which place they get allocated. Is there any good design pattern to address this problem? I have put debug statements and ADDREF and RELEASE and I can see reference count when release is called . In some cases its more than 1, that means pointer is

Passing const unique_ptr reference as parameter

China☆狼群 提交于 2020-01-04 05:51:09
问题 void f1(unique_ptr<A[]>& upA){ //some work... //callee can mess up smart pointer many ways for caller upA.reset(); //some work... } void f2(const unique_ptr<A[]>& upA){ //some work... //compiler stops callee from ruining smart pointer many ways for caller upA.reset(); //some work... } f1(upAArray); f2(upAArray); In the above code, calling f1 is dangerous because the callee can mess up the smart pointer by resetting it, releasing it, etc. Calling f2 is safe and everything works great. If

unique_ptr with an API that expects raw pointers?

夙愿已清 提交于 2020-01-04 02:36:19
问题 After about 10 years of using managed memory and functional languages, I'm finally coming home to C++, and smart pointers are confusing the heck out of me. Half of the documentation out there is still regarding the deprecated auto_ptr . I'm trying to implement this fairly straightforward Bullet "hello world" program: int _tmain(int argc, _TCHAR* argv[]) { auto bp = unique_ptr<btBroadphaseInterface>(new btDbvtBroadphase); auto cc = unique_ptr<btDefaultCollisionConfiguration>(new

How to maintain a weak pointer to a parent in C++?

偶尔善良 提交于 2020-01-02 19:26:08
问题 Is there a standard way of maintaining a weak pointer to a parent (which is created using a shared pointer) in a child object in C++? Essentially, I need to implement something on the lines of the following: Class B; Class A { ... private: B m_b; }; Class B { .... public: void SetParentPtr(const boost::shared_ptr<A>& a) { m_parentPtr = a; } private: boost::weak_ptr<A> m_parentPtr; }; In the above all instances of class B need to hold a weak pointer to their parent (i.e object of class A).

How to make all copies of a shared_ptr equal to another shared_ptr?

本秂侑毒 提交于 2020-01-02 18:34:14
问题 I cannot figure this out.. Looks like I'm missing something simple? What do I put in MakePointToSameValue so that at point (1) both b.ptr and c.ptr point to the same as a.ptr in other words, a.ptr.get() == b.ptr.get() == c.ptr.get() the value originally pointed to by b.ptr and c.ptr gets deleted ? struct Test { public: Test( int val ) : ptr( std::make_shared< int >( val ) ) { } void MakePointToSameValue( Test& other ) { //what do I put here? //other.ptr = this->ptr; //doesn't do it } private: