我今天问了一个关于 如何插入不同的问题同一向量数组中的对象类型 和我在那个问题中的代码是
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();
}
但后来他和许多其他人建议我最好使用 Boost 指针容器
或 shared_ptr
.在过去的 3 个小时里,我一直在阅读有关此主题的信息,但文档对我来说似乎相当先进.****谁能给我一个关于 shared_ptr
用法的小代码示例,以及为什么他们建议使用 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:我也阅读了一个代码示例,其中包括:
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
的 vector
消除了内存泄漏的可能性,因为你忘记遍历 vector 并且对每个元素调用 delete
.让我们逐行浏览一个稍微修改过的示例版本.
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::vector
并忘记结束大于号之间的空格.
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
对象的空向量.
Creates an empty vector of boost::shared_ptr<gate>
objects.
gate_ptr ptr(new ANDgate);
分配一个新的ANDgate
实例并将其存储到shared_ptr
中.单独执行此操作的原因是为了防止在操作抛出时可能出现的问题.在这个例子中这是不可能的.Boost 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
的析构函数确保了分配的内存被删除.这是避免内存泄漏的地方.std::vector
的析构函数确保为存储在向量中的每个元素调用 T
的析构函数.但是,指针的析构函数(例如,gate*
)不会删除您分配的内存.这就是您试图通过使用 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 的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!