while reading the book < thinking in c++ 2>, I got the codes below:
#include <stdlib.h>
#include <iostream>
#pragma warning(disable : 4297)
using namespace std;
class buffer
{
public:
explicit buffer(size_t) throw();
~buffer() throw();
private:
char *const p;
};
buffer::buffer(size_t const count)
try
: p(new char[count])
{
cout << "constructor goes well......" << endl;
}
catch (...)
{
throw "hello";
abort();
}
buffer::~buffer()
{
delete[] p;
cout << "destructor also goes well......" << endl;
}
static void do_something_with(buffer &) throw()
{
cout << "something is done !" << endl;
}
int main()
{
buffer b(100);
do_something_with(b);
return 0;
}
博主在阅读《thinking in c++ 2》时给出一段代码。代码定义了buffer类,包含构造函数和析构函数,构造函数中使用new分配内存,析构函数用delete释放内存,还定义了处理函数,最后在main函数中创建对象并调用处理函数。
795

被折叠的 条评论
为什么被折叠?



