scoped-ptr

Why scoped pointers in boost

故事扮演 提交于 2019-12-21 03:58:09
问题 What is the objective of scoped pointer? to my understanding, the scoped pointer manages the memory within a block of code. If i want to declare a variable within a block , i can just declare it on a stack and not worry about cleaning. 回答1: Not if it's of dynamic size or type. In addition, scoped pointers can be swapped, and in C++11 unique_ptr can be moved, so they're not strictly scoped. 回答2: Unlike stack-based data, scoped_ptr has a reset() member -- in other words, you can construct

Difference between boost::scoped_ptr<T> and std::unique_ptr<T>

心不动则不痛 提交于 2019-12-18 10:46:46
问题 Is the sole difference between boost::scoped_ptr<T> and std::unique_ptr<T> the fact that std::unique_ptr<T> has move semantics whereas boost::scoped_ptr<T> is just a get/reset smart pointer? 回答1: No, but that is the most important difference. The other major difference is that unique_ptr can have a destructor object with it, similarly to how shared_ptr can. Unlike shared_ptr , the destructor type is part of the unique_ptr 's type (the way allocators are part of STL container types). 回答2:

C++ polymorphism with boost scoped_ptr

回眸只為那壹抹淺笑 提交于 2019-12-06 06:51:42
问题 Why does the following code not allow foo(ptr) to be called ? #include <boost/scoped_ptr.hpp> struct A { virtual ~A() {} }; struct B: public A {}; void foo(boost::scoped_ptr<A>& a) {} void goo(A& a) {} int main() { boost::scoped_ptr<B> ptr(new B); foo(ptr); B b; goo(b); } The corresponding form where we pass references works as expected. Are we supposed not to do polymorphism with boost scoped_ptr ? g++ with boost 1.49 gives me: error: invalid initialization of reference of type ‘boost:

C++ polymorphism with boost scoped_ptr

怎甘沉沦 提交于 2019-12-04 12:57:07
Why does the following code not allow foo(ptr) to be called ? #include <boost/scoped_ptr.hpp> struct A { virtual ~A() {} }; struct B: public A {}; void foo(boost::scoped_ptr<A>& a) {} void goo(A& a) {} int main() { boost::scoped_ptr<B> ptr(new B); foo(ptr); B b; goo(b); } The corresponding form where we pass references works as expected. Are we supposed not to do polymorphism with boost scoped_ptr ? g++ with boost 1.49 gives me: error: invalid initialization of reference of type ‘boost::scoped_ptr<A>&’ from expression of type ‘boost::scoped_ptr<B>’ That's because foo , for some reason, takes a

Destructor not invoked when an exception is thrown in the constructor

邮差的信 提交于 2019-12-03 17:58:05
问题 Why is the destructor not invoked in this code? #include <boost/scoped_ptr.hpp> #include <iostream> class MyClass { boost::scoped_ptr<int> ptr; public: MyClass() : ptr(new int) { *ptr = 0; throw; std::cout<<"MyClass Allocated\n"; } ~MyClass() { std::cout<<"MyClass De-allocated\n"; } int increment() { return ++*ptr; } }; int main() { boost::scoped_ptr<MyClass> myinst(new MyClass); std::cout << myinst->increment() << '\n'; std::cout << myinst->increment() << '\n'; } EDIT From the answers, In

Why scoped pointers in boost

大兔子大兔子 提交于 2019-12-03 11:59:24
What is the objective of scoped pointer? to my understanding, the scoped pointer manages the memory within a block of code. If i want to declare a variable within a block , i can just declare it on a stack and not worry about cleaning. Not if it's of dynamic size or type. In addition, scoped pointers can be swapped, and in C++11 unique_ptr can be moved, so they're not strictly scoped. nobar Unlike stack-based data, scoped_ptr has a reset() member -- in other words, you can construct/destruct to your heart's content. With this, you can use a null pointer (technically operator unspecified-bool

shared_ptr with malloc and free

会有一股神秘感。 提交于 2019-12-03 03:44:05
问题 I have working in large application which contain c and cpp. The all files saved as cpp extension but the code is written in c- style. I mean it is define structure rather than class allocate memory through malloc and realloc and calloc.In recent They have installed boost library So I am planning to use into my existing code base So I have some following question. Can I use std::shared_ptr with malloc and free. If yes, can anyone point out me sample code base? Will it impact any functionality

shared_ptr with malloc and free

本小妞迷上赌 提交于 2019-12-02 17:10:40
I have working in large application which contain c and cpp. The all files saved as cpp extension but the code is written in c- style. I mean it is define structure rather than class allocate memory through malloc and realloc and calloc.In recent They have installed boost library So I am planning to use into my existing code base So I have some following question. Can I use std::shared_ptr with malloc and free. If yes, can anyone point out me sample code base? Will it impact any functionality if I create std::shared_ptr in my application and pass this pointer to another function, which uses

shared_ptr vs scoped_ptr

早过忘川 提交于 2019-11-28 15:47:46
问题 scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr . So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know you need to create a copy of your object or no. So the question is: besides the cases mentioned above, could we consider that shared_ptr is better (or recommended) to use instead of scoped_ptr . Does scoped_ptr work much faster from shared_ptr , or