c++ 模板類的 友元函數

來源:互聯網
上載者:User

標籤:功能   std   ons   code   nbsp   empty   []   max   nullptr   

  1 #pragma once  2 #include <iostream>  3   4 template <class T>  5 class stack  6 {  7     template <class Ty>  8     friend std::ostream& operator<<(std::ostream& os, const stack<Ty>& s);  9 public: 10     explicit stack<T>(int maxSize); 11     stack<T>(const stack<T>& s); 12     stack<T>(stack<T>&&) = delete; 13     stack<T>& operator=(const stack& s); 14     stack<T>& operator=(stack<T>&&) = delete; 15     ~stack<T>(); 16  17     void push(T e); 18     bool pop(T& e); 19     bool getTop(T& e) const; 20  21     bool isEmpty() const; 22     int getNumElems() const; 23  24 private: 25     T* elems; 26     int top; 27     int maxSize; 28     void overflowProcess(); 29 }; 30  31 template <class T> 32 std::ostream& operator<<(std::ostream& os,const stack<T>& s) 33 { 34     for (int i = 0; i <= s.top; i++) 35     { 36         std::cout << s.elems[i] << " "; 37     } 38     return os; 39 } 40  41 template <class T> 42 stack<T>::stack(int maxSize_): top(-1), maxSize(maxSize_) 43 { 44     this->elems = new T[this->maxSize]; 45 } 46  47 template <class T> 48 stack<T>::stack(const stack<T>& s) 49 { 50     this->top = s.top; 51     this->maxSize = s.maxSize; 52     this->elems = new T[this->maxSize]; 53     memcpy_s(this->elems, sizeof(T) * maxSize, s.elems, sizeof(T) * maxSize); 54 } 55  56 template <class T> 57 stack<T>& stack<T>::operator=(const stack& s) 58 { 59     if (this == &s) return *this; 60     if (this->elems != nullptr)delete[] elems; 61     this->top = s.top; 62     this->maxSize = s.maxSize; 63     this->elems = new T[maxSize]; 64     memcpy_s(this->elems, sizeof(T) * maxSize, s.elems, sizeof(T) * maxSize); 65     return *this; 66 } 67  68 template <class T> 69 stack<T>::~stack() 70 { 71     if (this->elems != nullptr) 72         delete[] elems; 73 } 74  75 template <class T> 76 void stack<T>::push(T e) 77 { 78     if (this->top == maxSize - 1) 79     { 80         overflowProcess(); 81     } 82     this->elems[++top] = e; 83 } 84  85 template <class T> 86 bool stack<T>::pop(T& e) 87 { 88     if (!isEmpty()) 89     { 90         e = this->elems[top--]; 91         return true; 92     } 93     return false; 94 } 95  96 template <class T> 97 bool stack<T>::getTop(T& e) const 98 { 99     if (!isEmpty())100     {101         e = elems[top];102         return true;103     }104     return false;105 }106 107 template <class T>108 bool stack<T>::isEmpty() const109 {110     return top == -1 ? true : false;111 }112 113 template <class T>114 int stack<T>::getNumElems() const115 {116     return top + 1;117 }118 119 template <class T>120 void stack<T>::overflowProcess()121 {122     T* new_elems = new T[maxSize + maxSize / 3];123     memcpy_s(new_elems, sizeof(T) * maxSize, this->elems, sizeof(T) * maxSize);124     delete[] elems;125     this->elems = new_elems;126 }

代碼長;懶得剪。。。。一個具備準系統的棧類;可以直接使用

裡面用到了:  模板友元函數   在類外定義的前面要加上template<class Ty> 以示區分

因此:  模板友元函數:類內定義       無需 template<class Ty>  /////其實這個還不確定,下次試試就知道了

                                     類內聲明,類外定義:需要tempalte<class Ty> 

          

c++ 模板類的 友元函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.