例如使用shared_ptr的? [英] Example to use shared_ptr?

查看:103
本文介绍了例如使用shared_ptr的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我今天问了一个问题关于<一个href=\"http://stackoverflow.com/questions/3475030/different-types-of-objects-in-the-same-vector-array\">How插入不同类型的对象在同一载体阵列并在这个问题我的code是

Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was

 gate* G[1000];
G[0] = new ANDgate() ;
G[1] = new ORgate;
//gate is a class inherited by ANDgate and ORgate classes
class gate
{
 .....
 ......
 virtual void Run()
   {   //A virtual function
   }
};
class ANDgate :public gate 
  {.....
   .......
   void Run()
   {
    //AND version of Run
   }  

};
 class ORgate :public gate 
  {.....
   .......
   void Run()
   {
    //OR version of Run
   }  

};      
//Running the simulator using overloading concept
 for(...;...;..)
 {
  G[i]->Run() ;  //will run perfectly the right Run for the right Gate type
 } 

和我想用这样的载体有人写了我应该做的:

and I wanted to use vectors so someone wrote that I should do that :

std::vector<gate*> G;
G.push_back(new ANDgate); 
G.push_back(new ORgate);
for(unsigned i=0;i<G.size();++i)
{
  G[i]->Run();
}

但随后他和其他许多人建议我会更好地使用升压指针容器结果
的shared_ptr 。我花了近3小时来阅读这个话题,但文档似乎pretty提前给我。 **谁能给我的shared_ptr 使用小code的例子,为什么他们建议使用的shared_ptr 。也有其他类型,如 ptr_vector ptr_list ptr_deque **

but then he and many others suggested that I would better use Boost pointer containers
or shared_ptr. I have spent the last 3 hours reading about this topic, but the documentation seems pretty advanced to me . **Can anyone give me a small code example of shared_ptr usage and why they suggested using shared_ptr. Also are there other types like ptr_vector, ptr_list and ptr_deque **

EDIT1:我读了code例子太多,其中包括:

I have read a code example too that included:

typedef boost::shared_ptr<Foo> FooPtr;
.......
int main()
{
  std::vector<FooPtr>         foo_vector;
........
FooPtr foo_ptr( new Foo( 2 ) );
  foo_vector.push_back( foo_ptr );
...........
}

和我不明白的语法!

推荐答案

使用的shared_ptr 矢量删除内存泄漏是因为你忘了走路的载体,并调用删除每个元素的可能性。让我们通过例子行由行略加修改的版本。

Using a vector of shared_ptr removes the possibility of leaking memory because you forgot to walk the vector and call delete on each element. Let's walk through a slightly modified version of the example line-by-line.

typedef boost::shared_ptr<gate> gate_ptr;

创建共享指针类型的别名。这就避免了从分型结果C ++语言的丑陋的std ::矢量&lt;提高:: shared_ptr的&LT;门&GT; &GT; 和遗忘之间的空间闭幕的大于号

Create an alias for the shared pointer type. This avoids the ugliness in the C++ language that results from typing std::vector<boost::shared_ptr<gate> > and forgetting the space between the closing greater-than signs.

    std::vector<gate_ptr> vec;

创建的空载体的boost :: shared_ptr的&LT;门方式&gt; 对象

    gate_ptr ptr(new ANDgate);

分配一个新的 ANDgate 实例,并将其存储到的shared_ptr 。的原因,用于分别操作这是prevent如果操作抛出可能发生的问题。这是不可能在这个例子。该升压的shared_ptr 最佳实践解释为什么它是一个的最佳实践的分配到一个独立的对象,而不是一个临时的。

Allocate a new ANDgate instance and store it into a shared_ptr. The reason for doing this separately is to prevent a problem that can occur if an operation throws. This isn't possible in this example. The Boost shared_ptr "Best Practices" explain why it is a best practice to allocate into a free-standing object instead of a temporary.

    vec.push_back(ptr);

这会在载体和复印件 PTR 进入一个新的共享指针。在的shared_ptr 的胆量的引用计数确保了 PTR 被安全地转移到载体内部分配的对象。

This creates a new shared pointer in the vector and copies ptr into it. The reference counting in the guts of shared_ptr ensures that the allocated object inside of ptr is safely transferred into the vector.

什么是没有解释的是,析构函数的shared_ptr&LT;门&GT; 确保分配的内存被删除。这是避免了内存泄漏的地方。为 STD析构函数::矢量&lt; T&GT; 确保了 T 调用析构函数存储在每一个元素向量。然而,析构函数指针(例如,门* )的不会删除你已分配的内存。这是你要设法避免使用什么的shared_ptr ptr_vector

What is not explained is that the destructor for shared_ptr<gate> ensures that the allocated memory is deleted. This is where the memory leak is avoided. The destructor for std::vector<T> ensures that the destructor for T is called for every element stored in the vector. However, the destructor for a pointer (e.g., gate*) does not delete the memory that you had allocated. That is what you are trying to avoid by using shared_ptr or ptr_vector.

这篇关于例如使用shared_ptr的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆