调查我是否需要使用auto_ptr [英] Investigating whether I need to use a auto_ptr

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

问题描述

我在这样的课程中有一个方法


class myClass

{

... ctros,dtor,等...


string myClassMethod()

{

string myString ="" ;;


...更多代码.....


myString ="新值" ;;


返回( myString);

}


};


所以我的问题是,这是安全还是退货字符串丢失

当执行超出范围时,在这种情况下我会更好地以/ b
以auto_ptr的形式传回一个字符串指针?


TIA,

Pep。

I have a method in a class like this

class myClass
{
... ctros, dtor, etc ...

string myClassMethod()
{
string myString = "";

... more code ....

myString = "new value";

return(myString);
}

};

So my question is, is this safe or will the returned string go missing
when execution goes out of scope, in which case I would be better of
passing back a string pointer in the form of a auto_ptr?

TIA,
Pep.

推荐答案

Pep写道:

....
Pep wrote:
....

string myClassMethod()

{
string myClassMethod()
{



....

....


>

所以我的问题是,这是安全还是返回的字符串去当执行超出范围时,缺少

,在这种情况下,我会更好地回复一个以auto_ptr形式返回字符串指针的

>
So my question is, is this safe or will the returned string go missing
when execution goes out of scope, in which case I would be better of
passing back a string pointer in the form of a auto_ptr?



std :: string类是一个可以复制的容器。你在这里做什么是
返回myString的副本,所以没有必要管理字符串生命周期的



另一方面,如果你写了:


std :: string& myClassMethod()


....这可能是未定义的。

the std::string class is a container that can be copied. What you''re
doing here is returning a copy of myString so there is no need to manage
the lifetime of the string.
On the other hand if you had written:

std::string & myClassMethod()

.... this would have been undefined.




Gianni Mariani写道:

Gianni Mariani wrote:

Pep写道:

...
Pep wrote:
...

string myClassMethod ()

{
string myClassMethod()
{



...

...



所以我的问题是,这是安全还是返回的字符串会丢失

当执行超出范围时,在这种情况下我会更好

传回一个字符串指针以auto_ptr的形式?

So my question is, is this safe or will the returned string go missing
when execution goes out of scope, in which case I would be better of
passing back a string pointer in the form of a auto_ptr?



std :: string类是一个可以复制的容器。你在这里做什么是
返回myString的副本,所以没有必要管理字符串生命周期的




另一方面,如果你写了:


std :: string& myClassMethod()


......这可能是未定义的。


the std::string class is a container that can be copied. What you''re
doing here is returning a copy of myString so there is no need to manage
the lifetime of the string.
On the other hand if you had written:

std::string & myClassMethod()

... this would have been undefined.



因此,在检查最终生产版本时我需要应用的规则

我的类库将确保其中的所有对象

图书馆有副本ctor?


另外,不要返回我不会尝试的参考文件吗?

干杯,

Pep。

So the rule I need to apply when checking the final production version
of my class library would be to ensure that all objects within the
library have a copy ctor?

Also, do not return references which I don''t attempt as a rule?

Cheers,
Pep.




Pep skrev:

Pep skrev:

Gianni Mariani写道:
Gianni Mariani wrote:



[snip]

[snip]


>

因此,在检查最终生产版本时我需要应用的规则

我的类库将确保
内的所有对象
图书馆有副本ctor?
>
So the rule I need to apply when checking the final production version
of my class library would be to ensure that all objects within the
library have a copy ctor?



第一条规则是:资源管理(无论是内存,句柄还是

其他任何东西)应该是自动的。特别是,释放资源

应该在析构函数中而不是其他地方。

The first rule is: resource management (be it memory, handles or
anything else) should be automatic. In particular, releasing a resource
should happen in a destructor and nowhere else.


>

另外,不要返回我不作为规则尝试的参考文献?
>
Also, do not return references which I don''t attempt as a rule?



永远不要返回本地数据的引用(包括指针)。 (

是规则#2),


/ Peter

Never return references (this includes pointers) to local data. (that
was rule #2),

/Peter


这篇关于调查我是否需要使用auto_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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