在C ++中,如何从函数返回对象数组?
通过返回一个std :: vector。
myobject *myfunc()
{
return new myobject[10];
}
But beware - you are transferring ownership of the array to the caller, might be a cause for memory leaks.
EDIT: returning a pointer to an array is a lot faster than returning a std::vector. If you are going to use a std::vector (as others have written), you may prefer to do it like this:
void myfunc(std::vector<myobject> &result)
{
result.resize(0);
for(int i=0;i<10;++i)
result.push_back(myobject());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.