默认情况下,对象是按值传递还是按引用传递?

发布于 2024-07-27 13:52:02 字数 333 浏览 8 评论 0原文

来自 C#,其中类实例通过引用传递(即,调用函数时传递引用的副本,而不是值的副本),我想知道这在 C++ 中是如何工作的。

下面这个例子,_poly = poly,是把poly的值复制到_poly,还是什么?

#include <vector>

class polynomial {
    std::vector<int> _poly;
public:
    void Set(std::vector<int> poly) { poly_ = poly; }
};

Coming from C#, where class instances are passed by reference (that is, a copy of the reference is passed when you call a function, instead of a copy of the value), I'd like to know how this works in C++.

In the following case, _poly = poly, is it copying the value of poly to _poly, or what?

#include <vector>

class polynomial {
    std::vector<int> _poly;
public:
    void Set(std::vector<int> poly) { poly_ = poly; }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

墨离汐 2024-08-03 13:52:02

poly 的值将被复制到 _poly 中——但在此过程中您将制作一个额外的副本。 更好的方法是通过 const 引用传递:

void polynomial::Set(const vector<int>& poly) {
    _poly = poly;                      
}

编辑我在有关复制和交换的评论中提到过。 实现您想要的另一种方法是

void polynomial::Set(vector<int> poly) { 
    _poly.swap(poly); 
}

这为您提供了额外的好处,即拥有强大的异常保证 而不是基本保证。 在某些情况下,代码也可能更快,但我认为这更多是一个好处。 唯一的问题是这段代码可能被称为“更难阅读”,因为人们必须意识到存在隐式副本。

poly's values will be copied into _poly -- but you will have made an extra copy in the process. A better way to do it is to pass by const reference:

void polynomial::Set(const vector<int>& poly) {
    _poly = poly;                      
}

EDIT I mentioned in comments about copy-and-swap. Another way to implement what you want is

void polynomial::Set(vector<int> poly) { 
    _poly.swap(poly); 
}

This gives you the additional benefit of having the strong exception guarantee instead of the basic guarantee. In some cases the code might be faster, too, but I see this as more of a bonus. The only thing is that this code might be called "harder to read", since one has to realize that there's an implicit copy.

是伱的 2024-08-03 13:52:02

这将执行整数向量的浅复制。 这通常会如您所期望的那样工作(_poly 最终将包含与 poly 相同的值)。

如果您有指针,您会看到一些奇怪的行为(因为它们将按值复制)。

通常,您希望通过 const 引用传递该参数:

void polynomial::Set( const vector<int>& poly )

在这种情况下,通过 const 引用传递不会影响结果,并且效率更高,因为它将消除传递到方法中的向量的不需要的副本。

This will do a shallow-copy of the vector of ints. This will generally work as you would expect (_poly will end up containing the same values as poly).

You would see some strange behaivor if you had pointers (as they would be copied by value).

In general, you would want to pass that parameter by const reference:

void polynomial::Set( const vector<int>& poly )

In this case, passing by const reference will not affect the outcome and will be more efficient since it will eliminate an unneeded copy of the vector being passed into the method.

哭了丶谁疼 2024-08-03 13:52:02

这将复制整个向量。 C++ 中的赋值是按值进行的。 如果要分配指针,则分配指针值。 一旦初始化,引用就不能被重新分配来引用另一个对象,因此它们的分配会改变引用对象。

This will copy the entire vector. Assignment is by value in C++. If you are assigning a pointer, the pointer value is assigned. References may not be reassigned to refer to another object once initialized, so assignment of them alters the referent object.

素衣风尘叹 2024-08-03 13:52:02

向量的复制运算符将复制向量的内容。

The copy operator for vectors will copy the contents of the vector over.

み格子的夏天 2024-08-03 13:52:02

有三种可能性:

按值传递

void someFunction(SomeClass theObject);

传递指针

void someFunction(SomeClass *theObject);

按引用传递

void someFunction(SomeClass &theObject);

There are three possibilities:

Pass by value

void someFunction(SomeClass theObject);

Pass a pointer

void someFunction(SomeClass *theObject);

Pass by reference

void someFunction(SomeClass &theObject);
万人眼中万个我 2024-08-03 13:52:02

您的矢量将被复制。

实际发生的情况是向量的“=”运算符已被重载以执行实际的复制。

Your vector will be copied.

What's actually going on is that the "=" operator of vector has been overloaded to do the actual copy.

八巷 2024-08-03 13:52:02

是的,您指向的行正在复制整个向量。 此外,函数调用上也会有一个副本,因为它不是 const。

基本上,如果向量有任何大小,这是非常昂贵的。

Yes, the line you point to is copying the entire vector. Furthermore, there will be a copy on the function call, as well, since that's not const.

Basically, if the vector has any size to it, this is VERY expensive.

除非您通过引用分配或传递参数(使用 & 前缀),否则您将按值传递。 对于类,这意味着使用该类型提供的或隐式生成的(浅)复制构造函数来构造对象的副本。 这可能会很昂贵,而且通常是不可取的。

在您的示例中,向量被复制两次 - 一次是在将其作为参数传递给 Set() 方法时,另一次是在将其分配给 _poly 成员时。

您可以通过引用传递向量来避免第一个副本:

void polynomial::Set(const vector<int>& poly) // passes the original parameter by reference
{
    _poly = poly; // still makes a copy
}

Unless you assign or pass a parameter by reference (using the & prefix) you are passing by value. For classes, this means that a copy of the object is constructed using either a supplied or implicitly generated (shallow) copy constructor for the type. This can be expensive - and is often undesirable.

In your example, the vector is copied twice - once when it is passed as a parameter to the Set() method, and again when it is assigned to the _poly member.

You could avoid the first copy by passing the vector by reference:

void polynomial::Set(const vector<int>& poly) // passes the original parameter by reference
{
    _poly = poly; // still makes a copy
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文