复制构造函数如何工作? [英] How does copy constructor work?

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

问题描述

复制构造函数在c ++中的工作方式

How does a copy constructor work in c++

错误1错误C2064:term不评估带有1个参数的函数c:\users\thuan\\ \\dropbox\homework\css 342\lab2\lab2\lab2\lab2.cpp 26

Error 1 error C2064: term does not evaluate to a function taking 1 arguments c:\users\thuan\dropbox\homework\css 342\lab2\lab2\lab2\lab2.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);)

注意:编译器错误告诉你表达式 (object2); 是一个函数调用表达式:你需要定义函数调用操作符 void InSet :: operator ; obj))使它编译(返回类型可以是任何其他,而不仅仅是 void ,这取决于你的需要。)如果你定义函数调用操作符,你将你的对象转换为所谓的函子

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

编译器错误函数接受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) code>无效,如果你只有(常规)构造函数是你列出的,除非你有(常规)构造函数的参数在类定义(声明构造函数的)的默认值,你没有include。

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.

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

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