使用赋值重载语法复制构造函数? [英] Copy Constructor with assignment overloading syntax?
问题描述
我正在编写大五(复制构造函数,复制赋值运算符,移动构造函数,移动赋值运算符,析构函数)。
I am working with writing the big five(copy constructor, copy assignment operator, move constructor, move assignment operator, destructor). And I've hit a bit of a snag with the copy constructor syntax.
说我有一个类foo,它有以下私有成员:
Say I have a class foo that has the following private members:
template<class data> // edit
class foo{
private:
int size, cursor; // Size is my array size, and cursor is the index I am currently pointing at
data * dataArray; // edit
}
如果我为这个任意大小写一个构造函数 X
它看起来像这样。
template<class data> // edit
foo<data>::foo(int X){
size = X;
dataArray = new data[size];
cursor = 0; // points to the first value
}
现在如果我想创建一个复制构造函数的另一个对象 bar
我需要进行以下操作:
Now if I wanted to make a copy constructor of another object called bar
I'd need to make the following:
template<class data> // edit
foo<data>::foo(foo &bar){
foo = bar; // is this correct?
}
假设我有重载 =
从以下代码:
Assuming I have the overloaded =
from the code below:
template<class data> // edit
foo<data>::operator=(foo &someObject){
if(this != someObject){
size = someObject.size;
cursor = someObject.cursor;
delete[] dataArray;
dataArray = new data[size];
for(cursor = 0; cursor<size-1;cursor++)
dataArray[cursor] = someObject.dataArray[cursor];
}
else
// does nothing because it is assigned to itself
return *this;
}
我的拷贝构造函数是否正确?或者应该 foo = bar
而不是 * this = bar
?
Is my copy constructor correct? Or should foo = bar
instead be *this = bar
?
我还是新的模板构造函数,所以如果我在代码中有任何错误,请让我知道我会纠正它。
I'm still new to templated constructors so if I made any errors in the code please let me know I will correct it.
编辑1:感谢Marcin提供的答案,我对上述代码进行了一些修改,使其更符合语法正确和注释他们与 //编辑
他们总结如下列表:
EDIT 1: Thanks to the answer provided below by Marcin I have made some edits to the code above to make it more syntatically correct and commented them with //edit
they are summarized in the list below:
- 以前
int * dataArray;
这错过了模板,应该是data * dataArray;
template< classname data>
,不正确的必须是 template< typename data>
或 < class data>
。 - previously
template<classname data>
, which is incorrect must betemplate <typename data>
ortemplate <class data>
for functions and classes respectively. - previously
int*dataArray;
this missuses the template and should bedata* dataArray;
推荐答案
您的foo类不会在内部使用
模板参数。我想你想在这里使用它:
Your foo class does not internally use data
template parameter. I suppose you wanted to use it here:
int * dataArray; // should be: data * dataArray;
您也不能使用 classname
关键字,但 typename
或 class
。您的代码中还有许多其他编译错误。
You also are not allowed to use classname
keyword but typename
or class
. You have also lots of other compile errors in your code.
您的副本构造函数错误,无法编译:
Your copy constructor is wrong, it will not compile:
foo = bar; // is this correct? - answer is NO
foo
名称,因此您的假设是正确的。 * this = someObject
这将工作(与额外的修补程序,至少dataArray必须设置为 nullptr
类变量将默认构造首先由复制构造函数仅被赋值运算符覆盖,因此其安静无效。如需更多资讯,请参阅:
foo
is a class name in this context, so your assumption is correct. *this = someObject
this would work (with additional fixes, at least dataArray must be set to nullptr
), but your class variables would be default constructed first by copy constructor only to be overwritten by assignment operator, so its quiet non efficent. For more read here:
这篇关于使用赋值重载语法复制构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!