返回新创建对象的最佳方法 [英] Best way to return newly created object

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

问题描述

大家好,


有时我会创建一个创建对象的函数并将其返回。

有些是集合,其他向量但是这是'不是很重要在这些

的情况下,我做了类似的事情:


vector< int> * f(){

vector< int> * v =新向量< int> ;;

返回v;

}


没有指针会有其他方法吗?


这会有效:

vector< int>& f(){

vector< int> v;

返回v;

}


???


干杯,


Paulo Matos

Hi all,

Sometimes I have a function which creates an object and returns it.
Some are sets, other vectors but that''s not very important. In these
cases I do something like this:

vector<int> * f() {
vector<int> * v = new vector<int>;
return v;
}

Would there be any other way without pointers?

Would this work:
vector<int>& f() {
vector<int> v;
return v;
}

???

Cheers,

Paulo Matos

推荐答案




pmatos写道:


pmatos wrote:
大家好,

有时我有一个创建对象并返回它的函数。
有些是集合,其他向量但是那个'不是很重要。在这些情况下,我做了类似的事情:

vector< int> * f(){
vector< int> * v = new vector< int> ;;
return v;
}

没有指针会有其他方法吗?

这会有效吗?
vector< int>& f(){
vector< int> v;
返回v;
}


干杯,

Paulo Matos
Hi all,

Sometimes I have a function which creates an object and returns it.
Some are sets, other vectors but that''s not very important. In these
cases I do something like this:

vector<int> * f() {
vector<int> * v = new vector<int>;
return v;
}

Would there be any other way without pointers?

Would this work:
vector<int>& f() {
vector<int> v;
return v;
}

???

Cheers,

Paulo Matos




不,那不行,当''f''返回时,'v''将被销毁,你将会b / b
有无效的参考。你应该考虑返回一个

std :: auto_ptr,或者其他一些智能指针。



No, that won''t work, ''v'' will be destroyed when ''f'' returns, you will
have an invalid reference. You should consider returning a
std::auto_ptr, or some other smart pointer.


pmatos写道:
pmatos wrote:
大家好,

有时我有一个创建对象并返回它的函数。
有些是集合,其他向量但不是很重要。在这些情况下,我做了类似的事情:

vector< int> * f(){
vector< int> * v = new vector< int> ;;
return v;
}

没有指针会有其他方法吗?

这会有效吗?
vector< int>& f(){
vector< int> v;
返回v;
}
Hi all,

Sometimes I have a function which creates an object and returns it.
Some are sets, other vectors but that''s not very important. In these
cases I do something like this:

vector<int> * f() {
vector<int> * v = new vector<int>;
return v;
}

Would there be any other way without pointers?

Would this work:
vector<int>& f() {
vector<int> v;
return v;
}




编号永远不要返回对本地对象的引用。


你能做的就是转身。让函数将向量作为

参数并填充它:


void f(向量< int>& vec)

{

//填充vec

}



No. Never return a reference to a local object.

What you could do is turn it around. Let the function take the vector as
parameter and fill it:

void f(vector<int>& vec)
{
//fill vec
}


" pmatos" < PO ** @ sat.inesc-id.pt> schrieb:
"pmatos" <po**@sat.inesc-id.pt> schrieb:
vector< int> * f(){
vector< int> * v =新向量< int> ;;
返回v;
}
vector<int> * f() {
vector<int> * v = new vector<int>;
return v;
}




我建议不要返回新创建的对象。没有人知道用指针做什么
。它是如何创建的?我们要删除它吗?它是堆上的
a指针还是只是现有对象的地址?


确实如果函数及其调用者不使用相同的堆

管理可能会导致严重的问题。如果您在DLL中创建一个对象并将指针返回到

应用程序,则会出现这种情况。如果两个模块都与静态运行时库链接

应用程序无法删除指针。


我建议在函数外部创建对象。你可以使用

函数来填充它。


void f(vector< int>& v)

{< br $>
//填充v ...

}


TM



I suggest not to return a newly created object. Nobody knows what to
do with the pointer. How it is created? Do we have to delete it? Is it
a pointer on the heap or just an address of an existing object?

Indeed if the function and its caller don''t use the same heap
management it could result in a serious problem. This is the case if
you create an object in a DLL and return a pointer up to an
application. If both modules are linked with a static runtime library
the application cannot delete the pointer.

I suggest to create the object outside the function. You can use the
function to fill it.

void f (vector<int>& v)
{
// fill v ...
}

T.M.


这篇关于返回新创建对象的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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