Define of class template template typename elemtype> class Binary iree public Binary) Binary Tree(const Binary Tree&) Binary Treeo Binary Tree& operator(const Binary Tree&) bool empty( return m root==0; j void insert( const elemtype& elem) void remove(const elemtype& elem) void clear private BTNode<elemtype>m root void copy (b TNode<elemtype>*dest, bTNode<elemtype>* src) //copy the src sub-tree to dest
Define of class template template < typename elemtype > class BinaryTree { public: BinaryTree(); BinaryTree(const BinaryTree&); ~BinaryTree(); BinaryTree& operator=(const BinaryTree&); bool empty() { return m_root = = 0; } void insert(const elemtype& elem); void remove(const elemtype& elem); void clear(); private: BTNode<elemtype>*m_root; void copy(BTNode<elemtype>* dest, BTNode<elemtype>* src); //copy the src sub-tree to dest };
Define of class template(cont u Defining an inline function in the class template is the same like in a normal class (empty) But how to define it outside the class template template typename elemtype inline Binary Treeselemtype> Binary TreeO: m root(0)i a template and template parameter list should be added a The reason why the second Binarytree need not a template parameter list is that, after the class scope operator, everything could be regarded as in the scope of the class template
Define of class template (cont.) ◼ Defining an inline function in the class template is the same like in a normal class (empty()) ◼ But how to define it outside the class template? template < typename elemtype > inline BinaryTree<elemtype>::BinaryTree() : m_root( 0 ) {}; ◼ template and template parameter list should be added ◼ The reason why the second BinaryTree need not a template parameter list is that, after the class scope operator, everything could be regarded as in the scope of the class template
Define of class template(cont template typename elemtype nline Binary Tree<elemtype> Binary Tree( const Binary Tree& rhs) i copy(m root, rhs. m root) template typename elemtype inline Binary Tree<elemtype>: Binary TreeO i clear template typename elemtype inline Binary Tree& Binary Tree<elemtype> operator=(const Binary Tree& rhs) if(this ! =&rhs) clear( copy(m root, rhs m root) return * this
Define of class template (cont.) template < typename elemtype > inline BinaryTree<elemtype>::BinaryTree(const BinaryTree& rhs) { copy(m_root, rhs.m_root); }; template < typename elemtype > inline BinaryTree<elemtype>::~BinaryTree() { clear(); }; template < typename elemtype > inline BinaryTree& BinaryTree<elemtype>::operator=(const BinaryTree& rhs) { if (this != &rhs) { clear(); copy(m_root, rhs.m_root); } return *this; };
Handle a type parameter u For a built-in and simple data type, there's almost no difference between by-value and by-reference in efficiency a For a class or complex data type, by-reference is usually used for efficiency But for type parameters of a template, we won't know in advance the real type it represents It is recommended declared them as const reference for the worst situation template typename elemtype> Binary Tree<elemtype>: find( const elemtype& value)
Handle a type parameter ◼ For a built-in and simple data type, there’s almost no difference between by-value and by-reference in efficiency ◼ For a class or complex data type, by-reference is usually used for efficiency ◼ But for type parameters of a template, we won’t know in advance the real type it represents ◼ It is recommended declared them as const reference for the worst situation template < typename elemtype > BinaryTree<elemtype>::find(const elemtype& value) {};
Handle a type parameter(cont.) Consider Initialization template< typename valtype template typename valtype inline BTNode<valtype> inline btnode<valtype> BTNode(const valtype& val) BTNode(const valtype& val) m Val m iCnt=1 Child=rchild=0 Child= rchild=o } Recommended //will call default constructor of matrix //will call copy constructor of Matrix /to m Val before entering the body, //to m Val only once //then the copy assignment operator BTNode <int> btni(20); //No difference in performance BINode<Matrix> btnm(mat8X8): /
Handle a type parameter (cont.) ◼ Consider Initialization template < typename valtype > inline BTNode<valtype>:: BTNode(const valtype& val) :m_Val(val) { m_iCnt = 1; lchild = rchild = 0; }; template < typename valtype > inline BTNode<valtype>:: BTNode(const valtype& val) { m_Val = val; m_iCnt = 1; lchild = rchild = 0; }; BTNode<int> btni(20); //No difference in performance BTNode<Matrix> btnm(mat8x8); //…? //will call copy constructor of Matrix //to m_Val only once //will call default constructor of Matrix //to m_Val before entering the body, //then the copy assignment operator Recommended