[C++筆記] 拷貝建構式(Copy Constructor) & 複製指派運算子(Copy Assignment Operator)

很容易搞混的兩種運用

比較

例子

Copy Constructor

T::T(const T& t)
{
hours = t.hours;
minutes = t.minutes;
len = t.len;
str = new char [len+1];
//this is why we should implement the copy constructor
}
---------------------
作者:Hosea14
来源:CSDN
原文:https://blog.csdn.net/bangdingshouji/article/details/72870027
版权声明:本文为博主原创文章,转载请附上博文链接!

Copy Assignment Operator

T& T::operator=(const T& t)
{
if(this == &t)//check if the sample address(it means the sample variable)
return *this;
delete []str;
//you also can check the string.len if not enough
len = new char [len+1];
std:strcpy(str, t.str);
hours = t.hours;
minutes = t.minutes;
return *this;
}
---------------------
作者:Hosea14
来源:CSDN
原文:https://blog.csdn.net/bangdingshouji/article/details/72870027
版权声明:本文为博主原创文章,转载请附上博文链接!

--

--