如果一个类需要析构函数,几乎可以肯定它也需要一个拷贝构造函数和一个拷贝赋值运算符。
以下讨论原因。
1没有拷贝拷贝构造函数和拷贝赋值运算符的情况
(1)main中拷贝初始化对象的情况
#include <iostream>
#include <string>
using namespace std;<pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
class S{
public:
string *ps;
S(const string &s = string()) :ps(new string(s)){ cout << "S()" << endl; }
~S(){ cout << "~S()" << endl; delete ps; }
};
S f(S hp)
{
S ret = hp;
cout << "f()中ps的地址为" << ret.ps << endl;
return ret;
}
int main()
{
S p("Some value");
cout << p.ps << endl;
f(p);
}
结果表明:
当类中需要自定义析构函数时,如果没有自定义拷贝构造函数,而是使用默认合成构造函数(合成拷贝构造函数)。main 函数中拷贝构造时,只会简单的拷贝指针的值,也就是说,两个对象的指针会指向同一个地址。这样在析构时就会调用两次析构函数,对同一个地址进行指针销毁,出现错误。
(2)函数调用的情况
#include <iostream>
#include <string>
using namespace std;
class S{
public:
string *ps;
S(const string &s = string()) :ps(new string(s)){ cout << "S()" << endl; }
~S(){ cout << "~S()" << endl; delete ps; }
};
S f(S hp)
{
S ret = hp;
cout << "f()中ps的地址为" << ret.ps << endl;
return ret;
}
int main()
{
S p("Some value");
cout << p.ps << endl;
f(p);
}
在函数调用时,简单的拷贝指针,析构时指针释放两次,出错!
2.加入自定义拷贝构造函数和拷贝赋值运算符
(1)两种情况分开看结果
#include <iostream>
#include <string>
using namespace std;
class S{
public:
string *ps;
S(const string &s = string()) :ps(new string(s)){ cout << "调用S(): " << "ps=" << ps << '\t' << "*ps=" << *ps << endl; }
~S(){ cout << "~S()" << endl; delete ps; }
S(const S& s1) :ps(new string(*(s1.ps))){ cout << "调用拷贝构造函数: " << "ps=" << ps << '\t' << "*ps=" << *ps << endl; }
S& operator=(const S& s1);
};
S&
S::operator=(const S& s1)
{
ps = new string(*(s1.ps));
cout << "调用拷贝赋值运算符: " << "ps=" << ps << '\t' << "*ps=" << *ps << endl;
return *this;
}
S f(S hp)
{
S ret = hp;
return ret;
}
int main()
{
S p("Some value");
cout << "自定义构造函数在main中的表现: " << p.ps << '\t' << *(p.ps) << endl << endl;
S q;
cout << "自定义构造函数在main中的表现: " << p.ps << '\t' << *(p.ps) << endl << endl;
q = p;
cout <<"赋值运算符在main函数中的表现:" << "ps=" << q.ps << '\t' << "*ps=" << *(q.ps) << endl << endl;
S a(p);
cout << "拷贝构造函数在main函数中的表现:" << "ps=" << a.ps << '\t' << "*ps=" << *(a.ps) << endl << endl;
/*
cout << "begin" << endl;
S b("hello");
cout << "mid" << endl;
f(b);
S c(b);
cout << "end" << endl;
*/
}
(2)第二种情况
#include <iostream>
#include <string>
using namespace std;
class S{
public:
string *ps;
S(const string &s = string()) :ps(new string(s)){ cout << "调用S(): " << "ps=" << ps << '\t' << "*ps=" << *ps << endl; }
~S(){ cout << "~S()" << endl; delete ps; }
S(const S& s1) :ps(new string(*(s1.ps))){ cout << "调用拷贝构造函数: " << "ps=" << ps << '\t' << "*ps=" << *ps << endl; }
S& operator=(const S& s1);
};
S&
S::operator=(const S& s1)
{
ps = new string(*(s1.ps));
cout << "调用拷贝赋值运算符: " << "ps=" << ps << '\t' << "*ps=" << *ps << endl;
return *this;
}
S f(S hp)
{
S ret = hp;
return ret;
}
int main()
{
/*
S p("Some value");
cout << "自定义构造函数在main中的表现: " << p.ps << '\t' << *(p.ps) << endl << endl;
S q;
cout << "自定义构造函数在main中的表现: " << p.ps << '\t' << *(p.ps) << endl << endl;
q = p;
cout <<"赋值运算符在main函数中的表现:" << "ps=" << q.ps << '\t' << "*ps=" << *(q.ps) << endl << endl;
S a(p);
cout << "拷贝构造函数在main函数中的表现:" << "ps=" << a.ps << '\t' << "*ps=" << *(a.ps) << endl << endl;
*/
cout << "begin" << endl;
S b("hello");
cout << "mid" << endl;
f(b);
S c(b);
cout << "end" << endl;
}
分析:
加入拷贝构造函数和拷贝赋值运算符后,问题解决。
故此标题:“需要析构函数的类也需要拷贝和赋值操作” 得证。
过程问题分析:
1.在上述f()调用时,可以看到,实参传入拷贝构造一次,函数内部创建一个对象拷贝构造一次,return 返回时再次拷贝, 总共三次。
因此,析构的时候应该拷贝三次。