摘要: 先看下面一段代码:class Node {};Node* CreateNode(){ }void Solve(){ Node *p=CreateNode(); //调用CreateNode函数 ... delete p; //释放资源}这样释放资源(delete)做法是正确的。但是,我们怎么能保证其他人在利用这个代码时在delete之前会不会使用continue语句或者return语句跳过delete,这样一来的话不就无法释放资源浪费资源了吗。有什么办法呢?即在程序结束之前一定会自动释放资源。auto_ptr(智能指针):其析构函数自动对其所指的函数调... 阅读全文
posted @ 2014-03-19 21:42 Mr. Ant 阅读(895) 评论(0) 推荐(0) 编辑
摘要: 现看这样一个程序:void logCall(const string& funcname) //标记记录{ cout <<funcname <<endl;}class Custom{ public: Custom(const Custom& p):name(p.name) { logCall("Custom copy constructor!") } Custom& operator=(const Custom& p) { logCall("Custom copy assignment operator!&q 阅读全文
posted @ 2014-03-19 17:18 Mr. Ant 阅读(221) 评论(0) 推荐(0) 编辑
摘要: 概念:编译器可以暗自为class创建default构造函数、copy构造函数、copy assignmengt构造函数,以及析构函数。比如你写下:struct Node{};这就好像你写下这样的代码:struct Node{ Node(){} //default构造函数 Node (const Node& p){} //copy构造函数 ~Node(){} //析构函数 Node& operator=(const Node& p){} // copy assignment操作符};唯有当这些函数被需要(被调用),它们才会被... 阅读全文
posted @ 2014-03-19 10:09 Mr. Ant 阅读(455) 评论(0) 推荐(0) 编辑