SHARE
TWEET

HTML Java script MYSQL programmers dev...(elmo)

a guest Jan 21st, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. You can actually define your template class inside a .template file rather than a .cpp file. Whoever is saying you can only define it inside a header file is wrong. This is something that works all the way back to c++ 98.
  2.  
  3. Don't forget to have your compiler treat your .template file as a c++ file to keep the intelli sense.
  4.  
  5. Here is an example of this for a dynamic array class.
  6.  
  7.  
  8.    #ifndef dynarray_h
  9.    #define dynarray_h
  10.    
  11.    #include <iostream>
  12.    
  13.    template <class T>
  14.    class DynArray{
  15.        int capacity_;
  16.        int size_;
  17.        T* data;
  18.    public:
  19.        explicit DynArray(int size = 0, int capacity=2);
  20.        DynArray(const DynArray& d1);
  21.        ~DynArray();
  22.        T& operator[]( const int index);
  23.        void operator=(const DynArray<T>& d1);
  24.        int size();
  25.        
  26.        int capacity();
  27.        void clear();
  28.        
  29.        void push_back(int n);
  30.        
  31.        void pop_back();
  32.        T& at(const int n);
  33.        T& back();
  34.        T& front();
  35.    };
  36.    
  37.    #include "dynarray.template" // this is how you get the header file
  38.    
  39.    #endif
  40.  
  41. Now inside you .template file you define your functions just how you normally would.
  42.  
  43.    template <class T>
  44.    DynArray<T>::DynArray(int size, int capacity){
  45.        if (capacity >= size){
  46.            this->size_ = size;
  47.            this->capacity_ = capacity;
  48.            data = new T[capacity];
  49.        }
  50.        //    for (int i = 0; i < size; ++i) {
  51.        //        data[i] = 0;
  52.        //    }
  53.    }
  54.    
  55.    template <class T>
  56.    DynArray<T>::DynArray(const DynArray& d1){
  57.        //clear();
  58.        //delete [] data;
  59.        std::cout << "copy" << std::endl;
  60.        this->size_ = d1.size_;
  61.        this->capacity_ = d1.capacity_;
  62.        data = new T[capacity()];
  63.        for(int i = 0; i < size(); ++i){
  64.            data[i] = d1.data[i];
  65.        }
  66.    }
  67.    
  68.    template <class T>
  69.    DynArray<T>::~DynArray(){
  70.        delete [] data;
  71.    }
  72.    
  73.    template <class T>
  74.    T& DynArray<T>::operator[]( const int index){
  75.        return at(index);
  76.    }
  77.    
  78.    template <class T>
  79.    void DynArray<T>::operator=(const DynArray<T>& d1){
  80.        if (this->size() > 0) {
  81.            clear();
  82.        }
  83.        std::cout << "assign" << std::endl;
  84.        this->size_ = d1.size_;
  85.        this->capacity_ = d1.capacity_;
  86.        data = new T[capacity()];
  87.        for(int i = 0; i < size(); ++i){
  88.            data[i] = d1.data[i];
  89.        }
  90.        
  91.        //delete [] d1.data;
  92.    }
  93.    
  94.    template <class T>
  95.    int DynArray<T>::size(){
  96.        return size_;
  97.    }
  98.    
  99.    template <class T>
  100.    int DynArray<T>::capacity(){
  101.        return capacity_;
  102.    }
  103.    
  104.    template <class T>
  105.    void DynArray<T>::clear(){
  106.        for( int i = 0; i < size(); ++i){
  107.            data[i] = 0;
  108.        }
  109.        size_ = 0;
  110.        capacity_ = 2;
  111.    }
  112.    
  113.    template <class T>
  114.    void DynArray<T>::push_back(int n){
  115.        if (size() >= capacity()) {
  116.            std::cout << "grow" << std::endl;
  117.            //redo the array
  118.            T* copy = new T[capacity_ + 40];
  119.            for (int i = 0; i < size(); ++i) {
  120.                copy[i] = data[i];
  121.            }
  122.            
  123.            delete [] data;
  124.            data = new T[ capacity_ * 2];
  125.            for (int i = 0; i < capacity() * 2; ++i) {
  126.                data[i] = copy[i];
  127.            }
  128.            delete [] copy;
  129.            capacity_ *= 2;
  130.        }
  131.        data[size()] = n;
  132.        ++size_;
  133.    }
  134.    
  135.    template <class T>
  136.    void DynArray<T>::pop_back(){
  137.        data[size()-1] = 0;
  138.        --size_;
  139.    }
  140.    
  141.    template <class T>
  142.    T& DynArray<T>::at(const int n){
  143.        if (n >= size()) {
  144.            throw std::runtime_error("invalid index");
  145.        }
  146.        return data[n];
  147.    }
  148.    
  149.    template <class T>
  150.    T& DynArray<T>::back(){
  151.        if (size() == 0) {
  152.            throw std::runtime_error("vector is empty");
  153.        }
  154.        return data[size()-1];
  155.    }
  156.    
  157.    template <class T>
  158.    T& DynArray<T>::front(){
  159.        if (size() == 0) {
  160.            throw std::runtime_error("vector is empty");
  161.        }
  162.        return data[0];
  163.        }
  164.  
  165. enoslay.elmo@yahoo.com.ph
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top