在C ++中返回对象或指针 [英] Returning an object or a pointer in C++

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

问题描述

在C ++中,我的方法应该返回对象还是指向对象的指针?如何决定?如果是运营商怎么办?我该如何定义?

In C++, should my method return an object or a pointer to an object? How to decide? What if it's an operator? How can I define?

还有另一件事-如果指针变成向量,返回后如何找出其大小?如果不可能,如我所想,我应该如何继续正确地返回不受此限制的数组?

And one more thing - if the pointer turns to be a vector, how can I find out its size after returned? And if it's impossible, as I think it is, how should I proceed to correctly return an array without this limitation?

推荐答案

在C ++中,我的方法应该返回对象还是指向对象的指针? 如何决定?

In C++, should my method return an object or a pointer to an object? How to decide?

自C ++ 11起,我们就在C ++中使用了移动语义,这意味着它与以前一样容易,现在也很容易按值返回.这应该是默认值.

Since C++11 we have move semantics in C++ which means that it as easy as before and now also fast to return by value. That should be the default.

如果它是运算符怎么办?我该如何定义?

What if it's an operator? How can I define?

许多运算符(例如operator=)通常将引用返回给*this

Many operators such as operator= normally return a reference to *this

X& X::operator=(X rhs); 

如果您想遵守通常的模式(并且应该这样做),则需要为每个操作员查询一下.从此处开始:运算符重载

You need to look that up for each operator if you would like to comply with the usual patterns (and you should). Start here: Operator overloading

Ed S指出,返回值优化也适用(甚至在C ++ 11之前),这意味着返回的对象经常不需要复制或移动.

As pointed out by Ed S. return value optimization also applies (even before C++11) meaning that often object you return need neither be copied or moved.

所以,现在这是退货的方式:

So, this is now the way to return stuff:

std::string getstring(){ 
   std::string foo("hello");
   foo+=" world";
   return foo;
}

我在这里制作了一个foo对象这一事实并不是我的意思,即使您只是做return "hello world";这也是要走的路.

The fact that I made a foo object here is not my point, even if you did just do return "hello world"; this is the way to go.

还有另一件事-如果指针变成矢量,我该怎么办 回来后找出它的大小吗?我认为如果不可能的话 是的,我应该如何正确地返回没有这个的数组 限制吗?

And one more thing - if the pointer turns to be a vector, how can I find out its size after returned? And if it's impossible, as I think it is, how should I proceed to correctly return an array without this limitation?

标准中的所有可复制或可移动类型(几乎是所有类型,例如vectorssets等)都是一样,除了少数例外.例如,std :: arrays不能从移动中受益.它们花费的时间与元素的数量成正比.在那里,您可以将其返回unique_ptr以避免复制.

The same goes for all copyable or movable types in the standard (these are almost all types, for example vectors, sets, and what not), except a few exceptions. For example std::arrays do not gain from moving. They take time proportional to the amount of elements. There you could return it in a unique_ptr to avoid the copy.

typedef std::array<int,15> MyArray;
std::unique_ptr<MyArray> getArray(){ 
  std::unique_ptr<MyArray> someArrayObj(new MyArray());
  someArrayObj->at(3)=5;
  return someArrayObj;
}

int main(){
  auto x=getArray();
  std::cout << x->at(3) <<std::endl; // or since we know the index is right: (*x)[3]
}

现在,为避免再写new(在极少数情况下,专家除外),您应该使用一个名为make_unique的辅助函数.这将极大地帮助异常安全,并且非常方便:

Now, to avoid ever writing new anymore (except for experts in rare cases) you should use a helper function called make_unique. That will vastly help exception safety, and is as convenient:

std::unique_ptr<MyArray> getArray(){ 
  auto someArrayObj=make_unique<MyArray>();
  someArrayObj->at(3)=5;
  return someArrayObj;
}

要获得更多动力和make_unique的(确实很短)实施,请在此处查看: make_unique和完美转发

For more motivation and the (really short) implementation of make_unique, have a look here: make_unique and perfect forwarding

现在make_unique是C ++ 14标准的一部分.如果没有,可以从STL的提案中找到并使用整个实现:

Now make_unique is part of the C++14 standard. If you don't have it, you can find and use the whole implementation from the proposal by S.T.L.:

有关如何执行此操作的想法示例

这篇关于在C ++中返回对象或指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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