复制构造函数是如何工作的?

How does copy constructor work?(复制构造函数是如何工作的?)
本文介绍了复制构造函数是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制构造函数在 C++ 中是如何工作的

How does a copy constructor work in c++

错误 1 ​​错误 C2064:术语不计算为采用 1 个参数的函数 c:users huandropboxhomeworkcss 342lab2lab2lab2lab2.cpp 26

Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:users huandropboxhomeworkcss 342lab2lab2lab2lab2.cpp 26

#include "intset.h"

int main() {
IntSet object1;
IntSet object2(9);  

object1(object2); //error here

   return 0;
}
//constructor
IntSet::IntSet(int a, int b, int c, int d, int e)
{
numOfArray++;
int tempArray[] = {a, b, c, d, e};
size = determineHighest(tempArray) + 1; 

arrayPtr = new bool[size]();

for (int i = 0; i < MAXINITIALSIZE; i++)
{
    if (tempArray[i] == -1)
        continue;
    arrayPtr[tempArray[i]]= true;
}   
}
//copy constructor
IntSet::IntSet(const IntSet &intsetObject)
{
numOfArray++;
size = intsetObject.size;

arrayPtr = new bool[size];

for (int i = 0; i < size; i++)
{
    if (intsetObject.arrayPtr[i])
        arrayPtr[i] = intsetObject.arrayPtr[i];
}
}

推荐答案

声明后

InSet object1;

名为 object1 的对象存在(通过默认构造函数创建).复制构造函数(就像常规构造函数一样)创建一个新对象.由于对象已经存在,表达式 object1(object2); 不能是复制构造函数调用.为此,您需要像这样声明变量:

the object named object1 exists (created via the default constructor). Copy constructor (just like a regular constructor) creates a new object. Since the object already exists, the expression object1(object2); cannot be a copy-constructor call. For that, you need to declare your variables like this:

InSet object2(9);
InSet object1(object2);

如果你想将object2复制到已经存在的object1,你需要一个赋值操作符(InSet&InSet::operator=(const InSet& other);)

If you wanted to copy object2 to the already existing object1, you will need an assignment operator (InSet& InSet::operator=(const InSet& other);)

注意: 编译器错误告诉你表达式 (object1(object2); 是一个函数调用表达式:你需要定义 function调用 operator (void InSet::operator()(const InSet& obj)) 使其编译(返回类型可以是其他任何类型,而不仅仅是 void,取决于你的需要.)如果你定义了函数调用操作符,你把你的对象变成了所谓的 functor

Note: The compiler error is telling you that the expression (object1(object2); is a function call expression: you will need to define the function call operator (void InSet::operator()(const InSet& obj)) to make it compile (the return type could be anything else, not just void, depending on your need.) If you define the function call operator, you turn your object into what is called a functor

编译器错误(term 不计算为带 1 个参数的函数)是正确的,但会误导 wrt.解决您的问题(但编译器无法知道您想要使用复制构造函数,而不是函数调用)

The compiler error (term does not evaluate to a function taking 1 arguments) is correct, but misleading wrt. to your problem (but there is no way for the compiler to know you wanted to use the copy constructor, instead of a function call)

注意:在许多编译器上,表达式

Note: On many compilers the expression

InSet object2(9);
InSet object1 = object2;

如果可用的话,还会导致调用复制构造函数(而不是默认构造对象然后调用赋值运算符)——这是一种可能的编译器优化.

also results in a call to the copy constructor (instead of default-constructing the object and then calling assignment operator on it), if available -- this is a possible compiler optimization.

注意:如果您拥有的唯一(常规)构造函数是您列出的构造函数,则声明 InSet object1;InSet object2(9); 无效,除非您在类定义(声明构造函数的地方)中具有(常规)构造函数参数的默认值,而您没有包含这些值.

Note: The declarations InSet object1; and InSet object2(9); are invalid if the only (regular) constructor you have is the one you listed, unless you have default values for the (regular) constructor's parameters in the class definition (where the constructor is declared), which you did not include.

这篇关于复制构造函数是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)