用C ++返回对象 [英] Returning an object in C++

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

问题描述

我是从C和Java为主的背景学习C ++的,我很好奇在没有复制对象的情况下用C ++返回对象的最佳方法是什么。根据我的理解,C ++ 11引入了右值引用(&&),以将数据从临时变量(反对复制)中移出。示例:

I am learning C++ from a background of mostly C and Java and I am curious on what is the best way to return an object in C++ without having to copy the object. From my understanding, C++11 introduced rvalue references (&&) to move data from a temporary variable (as oppose to copying). Example:

std::string getStr(){
   return "Hello";
}

std::string &&hello = getStr();

我能想到的另一种方法是使用共享指针。

Another way I could think of is using a shared pointer.

std::tr1::shared_ptr<std::string> getStr(){

    std::tr1::shared_ptr<std::string> hello(new std::string("Hello"));
    return hello;

}

auto hello = getStr();

我想也许右值引用会更好,但在使用前我想先征求第二意见它。哪个更好?

I am thinking maybe an rvalue reference is better but I'd like a second opinion first before I use it. Which is better?

如果不使用构造函数设置它们,是否建议将类中的字段设置为右值引用?示例:

Also is it recommended that fields in a class be rvalue references if they won't be set using the constructor? Example:

class StringHolder{

   public:
     std::string &&str;
};

StringHolder sh;
sh.str = getStr();

非常感谢你们!

推荐答案

该问题可能会重复存在。这是一个经常提出的问题。但是无论如何我还是要回答。

This question will probably be closed as a duplicate. It is an oft-asked question. However I would like to answer it anyway.

在C ++ 11中,当您是 std :: string ,您应该按值传递它,而不必太担心效率:

In C++11, when you are the client of an object like std::string, you should pass it around by value and not worry so much about efficiency:

std::string getStr(){
   return "Hello";
}

std::string hello = getStr();

在此级别,您无需关心右值引用。只知道 std :: string 可以从右值中复制(例如 getStr()的返回值)<强大高效。该副本实际上称为移动,由于您是从右值复制(匿名临时副本)而自动启用的。

At this level you do not need to be concerned with rvalue references. Just know that std::string can "copy" from rvalues (such as the return from getStr()) very efficiently. This "copy" is actually called a "move" and is enabled automatically because you are copying from an rvalue (an anonymous temporary).

不要尝试优化复制通过恢复引用计数。仅在需要共享所有权语义时使用引用计数。这句话并非总是如此。但是,要学习基础知识,就已经足够接近了,这是一个很好的经验法则。

Don't try to optimize copying by reverting to reference counting. Only use reference counting if you need shared ownership semantics. This statement isn't always true. But for learning the basics, it is close enough that it is a good rule of thumb to follow.

在设计您的类时,您需要开始担心右值引用需要按值传递:

You need to start worrying about rvalue references when you design your class that needs to be passed around by value:

class Widget
{
    // pointer to heap data
public:
    // ...
};

有关右值引用和移动语义的简要介绍, N2027 是一个不错的教程。 N2027太长而无法在此处粘贴,但足够短以方便阅读。 std :: string 遵循N2027中规定的基本原则,使您可以无罪地传递它。您可以在小部件中遵循相同的设计模式。

For a brief introduction to rvalue references and move semantics, N2027 is a decent tutorial. N2027 is too long to be pasted in here, but short enough to be an easy read. std::string follows the basics laid down in N2027 to enable you to pass it around by value guilt free. And you can follow those same design patterns in your Widget.

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

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