#include <iostream>
#include <string>
using namespace std;
class Example4 {
string* ptr;
public:
Example4 () : ptr (new string) {}
Example4 (const string& str) : ptr (new string (str)) {}
//C++中析构函数主要用于释放资源
~Example4 () {delete ptr;}
const string& content () const {return *ptr;}
};
int main(int argc, char const *argv[])
{
Example4 foo;
Example4 bar ("content");
cout << "bar's content: " << bar.content() << "\n";
return 0;
}
c++析构函数
最新推荐文章于 2024-03-11 20:06:43 发布