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.
这篇关于复制构造函数是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:复制构造函数是如何工作的?
基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
