当我们通过值将对象作为参数传递给方法时,为什么调用复制构造函数? [英] Why is the copy constructor called when we pass an object as an argument by value to a method?

查看:151
本文介绍了当我们通过值将对象作为参数传递给方法时,为什么调用复制构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按值将对象作为参数传递给函数时,为什么调用复制构造函数?请参见下面的代码:我正在将一个类的对象作为值作为参数传递给函数 display(),但是它在控件被击中之前正在调用复制构造函数 display()函数。我不明白为什么。

Why is the copy constructor called when I pass an object as an argument by value to a function? Please see my below code: I am passing an object of a class as an argument by value to a function display(), but it is calling the copy constructor before control is hitting the display() function. I don't understand why.

#include "stdafx.h"
#include <iostream>
using namespace std;

class ClassA
{
    private:
       int a, b;
    public:
       ClassA()
       {
           a = 10, b = 20;
       }
       ClassA(ClassA &obj)
       {
           cout << "copy constructor called" << endl;
           a = obj.a;
           b = obj.b;
       }
};
void display(ClassA obj)
{
   cout << "Hello World" << endl;
}
int main()
{
   ClassA obj;
   display(obj);
   return 0;
}


推荐答案

已经阐述了两个答案给出一点:

To elaborate the two answers already given a bit:

当您将变量定义为与某些其他变量相同时,基本上有两种可能性:

When you define variables to be "the same as" some other variable, you have basically two possibilities:

ClassA aCopy = someOtherA; //copy
ClassA& aRef = someOtherA; //reference

当然有const引用和rvalue引用,而不是非const左值引用。我想在这里指出的主要事情是, aCopy 独立于 someOtherA ,而 aRef 实际上与 someOtherA 相同,只是它的另一个别名(别名)。

Instead of non-const lvalue references, there are of course const references and rvalue references. The main thing I want to point out here is, that aCopy is independent of someOtherA, while aRef is practically the same variable as someOtherA, it's just another name (alias) for it.

与功能参数基本相同。当参数是引用时,它会在调用函数时绑定到参数,并且只是该参数的别名。就是说,您对参数执行的操作与参数对参数的执行:

With function parameters, it's basically the same. When the parameter is a reference, it gets bound to the argument when the function is called, and it's just an alias for that argument. That means, what you do with the parameter, you do with the argument:

void f(int& iRef) {
  ++iRef;
}

int main() {
  int i = 5;
  f(i); //i becomes 6, because iRef IS i
}

当参数为值时,它只是参数的一个副本,因此无论您对参数执行什么操作,参数都将保持不变。

When the parameter is a value, it is only a copy of the argument, so regardless of what you do to the parameter, the argument remains unchanged.

void f(int iCopy) {
  ++iCopy;
}

int main() {
  int i = 5;
  f(i); //i remains 5, because iCopy IS NOT i
}

按值传递时,该参数是一个新对象。由于它和论点不同,因此必须是独立的。创建作为参数副本的新对象,意味着调用复制构造函数或移动构造函数,具体取决于参数是左值还是右值。
在您的情况下,不需要通过值传递函数,因为您只读取参数。

When you pass by value, the parameter is a new object. It has to be, since it's not the same as the argument, it's independent. Creating a new object that is a copy of the argument, means calling the copy constructor or move constructor, depending on whether the argument is an lvalue or rvalue. In your case, making the function pass by value is unnecessary, because you only read the argument.

指南= http://herbsutter.com/2013/05/20/gotw-4-class-mechanics/ rel = nofollow noreferrer> GotW#4 :

There is a Guideline from GotW #4:


建议通过const&传递只读参数如果您只是要阅读它(而不是复制它)。

Prefer passing a read-only parameter by const& if you are only going to read from it (not make a copy of it).

这篇关于当我们通过值将对象作为参数传递给方法时,为什么调用复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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