Category Archives: Talking in Code

一些代码

日积月累: Uncopyable & Uninheritable in C++

如何在C++中阻止复制和继承呢,我做了个整理。 Uncopyable 有些类需要禁止被复制或者赋值,这需要显示的禁掉它们的拷贝构造和赋值函数。 摘自《Effective C++》Item 6: Explicitly disallow the use of compiler-generated functions you do not want class Uncopyable { protected: // allow construction Uncopyable() {} // and destruction of ~Uncopyable() {} // derived objects… private: Uncopyable(const Uncopyable&); // …but prevent copying Uncopyable& operator=(const Uncopyable&); }; class UncopyableExample: private Uncopyable { // THIS CLASS IS [...]

二叉查找树(Binary Search Tree)

今天下午闲着无聊,复习了一下“二叉查找树”,并用代码温习了一下。 对于二叉查找树,一般支持的操作有:查找关键字,最大值,最小值,前驱和后继等等的查询,对于高度为h的树,它们都可以在O(h)时间内完成。 BinaryTree.h #ifndef __BINARY_TREE_H__ #define __BINARY_TREE_H__ template < typename Key, typename Data > class BinaryTree; /*! 树的节点 */ template < typename Key, typename Data > class TreeNode { public: //! 默认构造函数 TreeNode() : m_pParent( NULL ), m_pLChild( NULL ), m_pRChild( NULL ) { } //! 构造函数 TreeNode( Key k, const Data & d ) [...]