练习13.11
为前面练习中的 HasPtr 类添加一个析构函数。
#ifndef ex13_11_h
#define ex13_11_h
#include <string>
class HasPtr
{
public:
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) {}
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) {}
HasPtr& operator=(const HasPtr &hp)
{
std::string *new_ps = new std::string(*hp.ps);
delete ps;
ps = new_ps;
i = hp.i;
return *this;
}
~HasPtr()
{
delete ps;
}
private:
std::string *ps;
int i;
};
#endif // !ex13_11_h
练习13.12
在下面的代码片段中会发生几次析构函数调用?
bool fcn(const Sales_data *trans, Sales_data accum)
{
Sa