如何检测boost:shared_ptr的NULL [英] How to detect NULL for boost:shared_ptr
问题描述
我正在玩增强指针并尝试包装以下代码
A * func(){
...
if(条件1){
返回a;
}
else
返回NULL;
}
现在我将a包装为shared_ptr(new A())并将函数更改为share_ptr< A>
func();
问题是shared_ptr不接受NULL。
在这种情况下我该怎么做?
谢谢
克里斯
Hi,
I am playing with boost pointer and try to wrap the following codes
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
}
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
How should I do it in this case?
Thanks
Chris
推荐答案
tr******@yahoo.com 写道:
我正在使用提升指针并尝试包装以下代码
A * func(){
...
if(条件1){
返回a;
}
else
返回NULL;
}
现在我换一个aa s shared_ptr(new A())并将函数更改为share_ptr< A>
func();
问题是shared_ptr不接受NULL。
I am playing with boost pointer and try to wrap the following codes
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
}
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
它是什么意思不接受它?你有编译器
错误吗?你有运行时异常吗?如果是后者,抓住它。如果
前者,请处理它(参见FAQ 5.8)。
What does it mean that it "will not accept" it? Do you get a compiler
error? Do you get a run-time exception? If the latter, catch it. If
the former, deal with it (see FAQ 5.8).
>
我该怎么办?在这种情况下做吗?
>
How should I do it in this case?
阅读常见问题解答。
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
Read the FAQ.
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
tr******@yahoo.com schrieb:
tr******@yahoo.com schrieb:
我正在使用升压指针并尝试包装以下代码
A * func (){
...
if(条件1){
返回a;
}
else
返回NULL;
}
现在我将a包装为shared_ptr(新的A())和更改功能为share_ptr< A>
func();
问题是shared_ptr不接受NULL。
>
在这种情况下我该怎么办?
Hi,
I am playing with boost pointer and try to wrap the following codes
A* func(){
...
if(condition 1 ){
return a;
}
else
return NULL;
}
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
How should I do it in this case?
您是否尝试返回这样的默认构造指针?
返回shared_ptr< A>();
-
Thomas
Did you try to return a default-constructed pointer like this?
return shared_ptr<A>();
--
Thomas
On 4 Sie,21:37,Thomas J. Gritzan" < phygon_antis ... @ gmx.dewrote:
On 4 Sie, 21:37, "Thomas J. Gritzan" <phygon_antis...@gmx.dewrote:
trade ... @ yahoo.com schrieb:
trade...@yahoo.com schrieb:
Hi,
我正在使用boost指针并尝试包装以下代码
I am playing with boost pointer and try to wrap the following codes
A * func(){
* * ...
* * if(条件1){
* * *返回a;
*}
* else
* *返回NULL;
A* func(){
* *...
* *if(condition 1 ){
* * * return a;
* }
* else
* *return NULL;
}
}
现在我将a包装为shared_ptr(new A ())并将函数更改为share_ptr< A>
func();
问题是shared_ptr不接受NULL。
Now I wrap a as shared_ptr(new A()) and change funcion as share_ptr<A>
func();
The problem is that shared_ptr will not accept NULL.
在这种情况下我该怎么办?
How should I do it in this case?
您是否尝试返回这样的默认构造指针?
* * return shared_ptr< A>() ;
-
Thomas
Did you try to return a default-constructed pointer like this?
* *return shared_ptr<A>();
--
Thomas
好吧,我不知道到底是什么你的意思是,因为shared_ptr是为了避免普通指针而使用
。但是,如果我理解正确的是什么
这就是你可以这样做的:
shared_ptr< Afunc()
{
shared_ptr< Ar; //自动设置为指向NULL
if(条件1)
{
shared_ptr< Ar(新A);
返回r; //我在某处读到了以避免创建新的shared_ptrs
在运行中比如return shared_ptr< A>(新A);"
}
其他
{
的shared_ptr<氩; //设置为自动指向
返回r;
}
}
// ...然后,代码中的某处:
shared_ptr< Atest = func();
if(test.get()== NULL)
做某事
我希望它有所帮助...
Well, I don''t know exactly what you mean, since shared_ptr is there to
avoid the ordinary pointers. However, if I understand correctly what
this is all about you can do it this way:
shared_ptr<Afunc()
{
shared_ptr<Ar; //automatically set to point to NULL
if(condition 1 )
{
shared_ptr<Ar(new A);
return r; //I read somewhere to avoid creating new shared_ptrs
"on the fly" like "return shared_ptr<A>(new A);"
}
else
{
shared_ptr<Ar; //set to point to NULL automatically
return r;
}
}
// ... and then, somewhere in code:
shared_ptr<Atest = func();
if(test.get() == NULL)
do sth
I hope it helps...
这篇关于如何检测boost:shared_ptr的NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!