Friend functions for C + + template classes

Source: Internet
Author: User
Tags class definition

1 #pragmaOnce2#include <iostream>3 4Template <classT>5 classStack6 {7Template <classTy>8Friend std::ostream&operator<< (std::ostream& OS,Conststack<ty>&s);9  Public:Ten     ExplicitStack<t> (intmaxSize); OneStack<t> (Conststack<t>&s); AStack<t> (stack<t>&&) =Delete; -stack<t>&operator=(Conststack&s); -stack<t>&operator= (stack<t>&&) =Delete; the~stack<t>(); -  -     voidpush (T e); -     BOOLPop (t&e); +     BOOLGetTop (t& e)Const; -  +     BOOLIsEmpty ()Const; A     intGetnumelems ()Const; at  - Private: -t*Elems; -     inttop; -     intmaxSize; -     voidoverflowprocess (); in }; -  toTemplate <classT> +std::ostream&operator<< (std::ostream& OS,Conststack<t>&s) - { the      for(inti =0; I <= s.top; i++) *     { $Std::cout << S.elems[i] <<" ";Panax Notoginseng     } -     returnos; the } +  ATemplate <classT> theStack<t>::stack (intMaxsize_): Top (-1), MaxSize (maxsize_) + { -      This->elems =Newt[ This-MaxSize]; $ } $  -Template <classT> -Stack<t>::stack (Conststack<t>&s) the { -      This->top =S.top;Wuyi      This->maxsize =s.maxsize; the      This->elems =Newt[ This-MaxSize]; -memcpy_s ( This->elems,sizeof(T) * maxSize, S.elems,sizeof(T) *maxSize); Wu } -  AboutTemplate <classT> $stack<t>& Stack<t>::operator=(Conststack&s) - { -     if( This= = &s)return* This; -     if( This->elems! = nullptr)Delete[] elems; A      This->top =S.top; +      This->maxsize =s.maxsize; the      This->elems =NewT[maxsize]; -memcpy_s ( This->elems,sizeof(T) * maxSize, S.elems,sizeof(T) *maxSize); $     return* This; the } the  theTemplate <classT> thestack<t>::~Stack () - { in     if( This->elems! =nullptr) the         Delete[] elems; the } About  theTemplate <classT> the voidStack<t>::p ush (T e) the { +     if( This->top = = MaxSize-1) -     { the overflowprocess ();Bayi     } the      This->elems[++top] =e; the } -  -Template <classT> the BOOLStack<t>::p op (t&e) the { the     if(!isEmpty ()) the     { -E = This->elems[top--]; the         return true; the     } the     return false;94 } the  theTemplate <classT> the BOOLStack<t>::gettop (t& e)Const98 { About     if(!isEmpty ()) -     {101E =Elems[top];102         return true;103     }104     return false; the }106 107Template <classT>108 BOOLStack<t>::isempty ()Const109 { the     returntop = =-1?true:false;111 } the 113Template <classT> the intStack<t>::getnumelems ()Const the { the     returnTop +1;117 }118 119Template <classT> - voidStack<t>:: overflowprocess ()121 {122t* New_elems =NewT[maxsize + maxSize/3];123memcpy_s (New_elems,sizeof(T) * maxSize, This->elems,sizeof(T) *maxSize);124     Delete[] elems; the      This->elems =New_elems;126}

The code is long; it's too lazy to cut .... A stack class with basic functions, which can be used directly

It is used in: template friend function is defined outside the class to be added Template<class ty> to differentiate

Therefore: template friend function: In-class definition does not need template<class ty>/////actually this is not sure, next time try to know

Intra-class declaration, out-of-class definition: Requires Tempalte<class ty>

Friend functions for C + + template classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.