C + + classes template (class template) detailed

Source: Internet
Author: User
Tags include throw exception

A class template (class template) needs to add a template parameter (template parameter), that is, add "template <template t>" at the front;

Place all the types of templates that need to be used instead of "T"; Need to fill in with "class<t>", specify template parameters;

You also need to add the template parameter "template <template t>" of the class when you define a member function of the class,

And when declaring a function's attribution class, the class needs to be converted to a template class, that is, "class::" to "CLASS<T>::";

If you are using this class object, the current object, in the class, you can add no template parameters (add no problem);

The rest of the note initializes the use of the list "Initializer_list", and prefix + + or--with suffix + + or--the difference between overloads;

The code is as follows:

* * * cppprimer.cpp * * Created on:2013.11.21 * author:caroline//*eclipse CDT, gcc 4.8.1*/ #include <iostream> #include <vector> #include <initializer_list> #include <memory> #i  
Nclude <cstddef> Template <typename t> class blobptr;  
      
Template <typename t> class Blob;  
/*blob:binary Large object*/Template <typename class Blob {friend class t>  
    Public:typedef T Value_type;  
    typedef typename STD::VECTOR&LT;T&GT;::SIZE_TYPE Size_type;  
    Blob (); Blob (std::initializer_list<t> il);  
    You can use the initialization list, {} size_type size () const {return data->size ();}  
    BOOL Empty () const {return data->empty ();}  
    void push_back (const t &t) {data->push_back (t);}  
    void Push_back (t &&t) {data->push_back (Std::move (t));}//Right value operation void Pop_back ();  
    t& back (); t& operator[] (size_typei);  
    private:std::shared_ptr<std::vector<t> > data; void Check (Size_type i, const std::string &msg) const;  
      
Validates the given index}; Template <typename t> Blob<t>::blob (): Data (std::make_shared<std::vector<t>> ()) {} Te Mplate <typename t> blob<t>::blob (std::initializer_list<t> il): Data (std::make_shared< std::vector<t> > (IL) {}/* verifies the given index */template <typename t> void Blob<t>::check (size_type  
I, const std::string &msg) Const {if (I >= data->size ()) throw Std::out_of_range (msg);//Throw exception  
    Template <typename t> t& blob<t>::back () {check (0, "Back on Empty Blob");  
return Data->back ();  } template <typename t> t& blob<t>::operator[] (size_type i) {check (I, "Subscript out  
    of range ");  
return (*data) [i]; } template <typename t>  
void Blob<t&gt::p op_back () {check (0, "Pop_back on Empty Blob");  
Data->pob_back (); Template <typename t> class Blobptr {public:blobptr (): Curr (0) {} blobptr (blob<t > &a, size_t sz=0): Wptr (A.data), Curr (SZ) {} t& operator* () const {Auto p = check (Curr, "  
        Dereference past End ");  
    return (*P) [Curr]; } blobptr& operator++ ();  
    Prefix operator blobptr& operator--(); Blobptr operator++ (int);  
Suffix operator blobptr operator--(int);  
    Private:std::shared_ptr<std::vector<t>> Check (std::size_t, const std::string&) const;  
    Std::weak_ptr<std::vector<t>> wptr;  
std::size_t Curr;  
      
};  Template <typename t> std::shared_ptr<std::vector<t>> blobptr<t>::check (std::size_t i, const std::string& msg) Const {AUTO ret = Wptr.lock ();///Determine if WPTR is bound with a blob if (!ret) throw std::runtime_error ("unbound blobptr");  
    if (I >= ret->size ()) throw Std::out_of_range (msg);  
return ret; } template <typename t> blobptr<t>& blobptr<t>::operator++ () {Check (Curr, "incre ment past end of Blobptr ");  
    First judge and then add ++curr;  
return *this; } template <typename t> blobptr<t>& blobptr<t>::operator--() {--curr; 0, the reduction is a large integer check (curr, "decrement past begin of the Blobptr");  
First minus then judge return *this; } template <typename t> blobptr<t> blobptr<t>::operator + + (int) {BLOBPTR ret = *this  
    ; ++*this;  
Use the overloaded prefix + + RETURN ret; } template <typename t> blobptr<t> blobptr<t>::operator-(int) {BLOBPTR ret = *this  
    ; --*this;  
Use the overloaded prefix-return ret;  
    int main (void) {std::cout << "Hello mystra!" << Std::endl; Blob<int> ia;  
    Blob<int> ia2 = {0, 1, 2, 3, 4};  
    Std::cout << "ia2[2] =" << ia2[2] << Std::endl;  
    blobptr<int> pia = ia2;  
    Std::cout << "* (++pia) =" << * (++pia) << Std::endl;  
return 0; }

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.