返回结构 [英] Returning structures

查看:79
本文介绍了返回结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于某些输入创建函数的最佳方法是什么?

返回结构或null值如果结构不能为
制作?


问题是结构已在函数内部创建,

我听说返回指向本地创建结构的指针

是不安全的。


-

我只是海市蜃楼。

解决方案
" kelvSYC" <柯***** @ no.email.shaw.ca>写道...

创建函数的最佳方法是什么,基于某些输入,如果不能构造结构,则返回结构或空值?<问题是结构已在函数内部创建,
我听说返回指向本地创建的结构的指针是不安全的。




正确。但返回指向动态创建的结构的指针

完全没问题。只要确保调用者在他们不再需要它时正确处理它就可以正确处理它。


SomeStruct * func(){

...

SomeStruct * toreturn = 0;

if(everything_is_OK)

toreturn = new SomeStruct(arguments);

...

返回返回;

}


void foo(){

SomeStruct * pSS = func();

if(pSS){

//做某事

删除pSS;

}

}


Victor


kelvSYC写道:

创建函数的最佳方法是什么,基于某些输入,返回结构或空值
如果不能构造结构?我听说返回指向本地创建的结构的指针是不安全的。




struct X {/ *。 。 。 * /};


X f(无效){

X x;

//修改x

返回x;

}


这是最安全的*和*最有效的方式。

你可以调用它像这样:


X x = f();

如果你的C ++编译器实现了
和* no *拷贝将是必需的>
命名返回值优化(NRVO)。


" E. Robert Tisdale <,E ************** @ jpl.nasa.gov>。写道:

kelvSYC写道:

创建函数的最佳方法是什么,根据一些输入,
返回结构或一个空值
如果不能制作结构?

问题是结构已在函数内创建,
我听说返回指针本地创建的结构是不安全的。



struct X {/ *。 。 。 * /};

X f(无效){
X x;
//修改x
返回x;
}
<这是最安全的*和*最有效的方式。
你可以像这样调用它:

X x = f();

和*如果您的C ++编译器实现了命名返回值优化(NRVO),则不需要*副本。




返回不同内容的要求如何(如0)

表示对象创建是不可能的?


你真的看过你回复的帖子吗?


V


What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I''ve heard that returning a pointer to a locally created structure
is unsafe.

--
I am only a mirage.

解决方案

"kelvSYC" <ke*****@no.email.shaw.ca> wrote...

What is the best way to create functions that, based on some input,
return either structures or a null value if the structure cannot be
made?

The problem is that the structure has be created inside the function,
and I''ve heard that returning a pointer to a locally created structure
is unsafe.



Correct. But returning a pointer to a dynamically created structure
is perfectly fine. Just make sure that the caller disposes of it
correctly when they don''t need it any longer.

SomeStruct* func() {
...
SomeStruct* toreturn = 0;
if (everything_is_OK)
toreturn = new SomeStruct(arguments);
...
return toreturn;
}

void foo() {
SomeStruct* pSS = func();
if (pSS) {
// do something
delete pSS;
}
}

Victor


kelvSYC wrote:

What is the best way to create functions that, based on some input,
return either structures or a null value
if the structure cannot be made?

The problem is that the structure has be created inside the function,
and I''ve heard that returning a pointer to a locally created structure
is unsafe.



struct X { /* . . . */ };

X f(void) {
X x;
// modify x
return x;
}

This is the safest *and* most efficient way.
You can invoke it like this:

X x = f();

and *no* copies will be required if your C++ compiler implements
the Named Return Value Optimization (NRVO).


"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote...

kelvSYC wrote:

What is the best way to create functions that, based on some input,
return either structures or a null value
if the structure cannot be made?

The problem is that the structure has be created inside the function,
and I''ve heard that returning a pointer to a locally created structure
is unsafe.



struct X { /* . . . */ };

X f(void) {
X x;
// modify x
return x;
}

This is the safest *and* most efficient way.
You can invoke it like this:

X x = f();

and *no* copies will be required if your C++ compiler implements
the Named Return Value Optimization (NRVO).



What about the requirement to return something different (like 0)
to indicate the object creation is impossible?

Do you actually read the posts you reply to?

V


这篇关于返回结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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