添加指向容器的指针 [英] Adding pointer to container
问题描述
我有一个std ::指向基类的指针说
std :: vector< element * m_elements;
如何进行跟随异常安全
功能()
{
元素* e =新DerivedElement;
m_elements。 push_back(element);
}
推回操作可以抛出所以我可以泄漏元素如果它抛出
i想到的这样做但它仍然有与e.get()相同的问题
首先被执行
然后如果推回抛出我泄漏指针。怎么解决??我
不能使用
提升这个项目所以shared_ptr不是一个选项
function()
{
std :: auto_ptr< elemente(new DerivedElement);
m_elements.push_back(e.get());
}
Hi, i have a std::vector of pointers to base classes say
std::vector<element*m_elements;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push_back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can''t use
Boost on this project so the shared_ptr is not an option
function()
{
std::auto_ptr <elemente(new DerivedElement);
m_elements.push_back(e.get());
}
推荐答案
tech写道:
tech wrote:
我有一个std ::指向基类的指针说明
std :: vector< element * m_elements;
如何使followin异常安全
>
函数()
{
元素* e =新DerivedElement;
m_elements.push_back(元素);
}
推回操作可以抛出所以我可以泄漏元素如果它抛出
i想到这样做但它仍然有相同问题为e.get()
首先被执行
然后如果推回投掷我泄漏指针。怎么解决??我
不能使用
提升这个项目所以shared_ptr不是一个选项
Hi, i have a std::vector of pointers to base classes say
std::vector<element*m_elements;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push_back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can''t use
Boost on this project so the shared_ptr is not an option
那么滚动你自己的简单shared_ptr指针。
-
Ian Collins。
Then roll your own simple shared_ptr pointer.
--
Ian Collins.
" tech" ; < na ************ @ googlemail.com写信息
news:01 ****************** **************** @ z66g2000 hsc.googlegroups.com ...
"tech" <na************@googlemail.comwrote in message
news:01**********************************@z66g2000 hsc.googlegroups.com...
我有一个std ::指向基类的指针向量说
std :: vector< element * m_elements;
如何使followin异常安全
function()
{
element * e = new DerivedElement;
m_elements.push_back(element);
}
推回操作可以抛出所以我可以泄漏元素如果它抛出
i想到这样做但它仍然有与e相同的问题.get()
首先被执行
然后如果推回投掷我泄漏指针。怎么解决??我
不能使用
提升这个项目所以shared_ptr不是一个选项
function()
{
std :: auto_ptr< elemente(new DerivedElement);
m_elements.push_back(e.get());
}
Hi, i have a std::vector of pointers to base classes say
std::vector<element*m_elements;
how do i make the followin exception safe
function()
{
element* e= new DerivedElement;
m_elements.push_back(element);
}
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can''t use
Boost on this project so the shared_ptr is not an option
function()
{
std::auto_ptr <elemente(new DerivedElement);
m_elements.push_back(e.get());
}
您可以这样做:
function()
{
std :: auto_ptr< elemente(new DerivedElement);
m_elements.push_back(e.get());
e。 release();
}
You could do something like:
function()
{
std::auto_ptr <elemente(new DerivedElement);
m_elements.push_back(e.get());
e.release();
}
6月11日,12:15 * pm,Chris Thomasson < cris ... @ comcast.netwrote:
On Jun 11, 12:15*pm, "Chris Thomasson" <cris...@comcast.netwrote:
" tech" < naumansulai ... @ googlemail.com写信息
新闻:01 *********************** *********** @ z66g2000 hsc.googlegroups.com ...
"tech" <naumansulai...@googlemail.comwrote in message
news:01**********************************@z66g2000 hsc.googlegroups.com...
我有一个std ::指向基类的指针*说
std :: vector< element * m_elements;
如何使followin异常安全
Hi, i have a std::vector of pointers to base classes *say
std::vector<element*m_elements;
how do i make the followin exception safe
function()
{
* * element * e = new DerivedElement;
* * m_elements.push_back(element);
}
function()
{
* *element* e= new DerivedElement;
* *m_elements.push_back(element);
}
推回操作可以抛出所以我可以泄漏元素如果它抛出
i想到这样做但它仍然有与e.get相同的问题()
首先被执行
然后如果推回投掷我泄漏指针。怎么解决??我
不能使用
提升这个项目所以shared_ptr不是一个选项
the push back operation can throw so i can leak element if it throws
i thought of doing this but it still has the same problem as e.get()
gets executed first
then if the push back throws i leak the pointer. How to solve?? I
can''t use
Boost on this project so the shared_ptr is not an option
函数()
{
* std :: auto_ptr< elemente(new DerivedElement);
* m_elements.push_back(e .get());
}
function()
{
* std::auto_ptr <elemente(new DerivedElement);
* m_elements.push_back(e.get());
}
您可以这样做:
功能()
{
* * std :: auto_ptr< elemente(new DerivedElement);
* * m_elements.push_back(e.get ());
* * e.release();
} - 隐藏引用的文字 -
- 显示引用文字 - 隐藏引用文字 -
- 显示引用文字 -
You could do something like:
function()
{
* *std::auto_ptr <elemente(new DerivedElement);
* *m_elements.push_back(e.get());
* *e.release();
}- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
回复Chris Thomasson >
std :: auto_ptr< elemente(new DerivedElement);
m_elements.push_back(e.get());
e.release();
std::auto_ptr <elemente(new DerivedElement);
m_elements.push_back(e.get());
e.release();
但是这就是我上面没有e.>
底部的e.release(),如果是m_elements.push_back(e.get) ());因为e.get()已经放弃了指针,所以抛出
然后如果
推回投掷肯定会泄漏指针。
或者你是说e.get()返回指针的副本
并且auto_ptr仍然挂在它上面。在那种情况下
它会起作用并感谢你
But this is what i had above without the e.release() at the
bottom, if m_elements.push_back(e.get()); this throws
as the e.get() has already given up the pointer then if the
push back throws surely i will leak the pointer.
Or are you saying the e.get() returns a copy of the pointer
and the auto_ptr still hangs onto it. In that case
it would work and thanks to you
这篇关于添加指向容器的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!