Application of C + + templates

Source: Internet
Author: User

Tag: Generate construct virtual address release an ace let SOF

Requirements: Analogy array class, except that the array type is no longer integer, float, etc., or it can be a class.

1. Create a template class

Header file

#ifndef myvector_h#define myvector_h#include <iostream> #include "Teacher.h" using namespace Std;template < TypeName T>class myvector{public:    myvector ();    void init (int size=0);   Constructor    myvector (const myvector &obj);  Copy constructor    ~myvector ();    destructor    const int Getlen () const;    Const t* Getspace () const {return m_space;}    t& operator [] (int index);    myvector<t> &operator = (const myvector<t> &obj);p rivate:    T *m_space;    int M_len;}; #endif//Myvector_h

Resource file

#include "myvector.h" #include "Teacher.h" Template<typename t>myvector<t>::myvector () {m_space = NULL; M_len = 0;}    Template<typename t>void myvector<t>::init (int size) {m_space=new t[size]; This->m_len=size;}    Template<typename t>myvector<t>::myvector (const myvector &obj)//copy constructor {M_len=obj.m_len;    cout<<m_len<<endl;    M_space=new T[m_len];    for (int i = 0; i < M_len; ++i) {m_space[i]=obj.m_space[i];    }}template<typename t>myvector<t>::~myvector ()//destructor {cout<< "destory ..." <<endl;        if (m_space! = NULL) {delete [] m_space;        M_space=null;    m_len=0; }}template<typename t>const int Myvector<t>::getlen () const{return m_len;} Template<typename t>t& Myvector<t>::operator [] (int index) {return m_space[index];} Template<typename t>myvector<t> & myvector<t>::operator= (const myvector<t> &obj) {   First release old memory if (m_space! = null) {cout<< "not NULL" <<endl;        Delete[] M_space;        M_space=null;    m_len=0;    }//Allocate memory by parameter int length = Obj.getlen ();    M_space=new T[length];    this->m_len=length;    for (int i = 0; i < M_len; ++i) {m_space[i]=obj.getspace () [i]; } return *this;}

2. Experimental class

Header file

#ifndef teacher_h#define teacher_h#include<iostream>using namespace Std;class teacher{public:    TEACHER ();    Teacher (char *name,int age);    Teacher (const Teacher &teacher);    ~teacher ();    void Printteacher ();    Friend Ostream &operator << (ostream &out,teacher &teacher);    Teacher &operator = (const Teacher &teacher);p rivate:    int age;    char *name;}; #endif//Teacher_h

Resource file

#include "Teacher.h" #include <iostream> #include <string.h>using namespace std;    Teacher::teacher () {age=0;    Name=new Char[1]; strcpy (Name, "");}    Teacher::teacher (char *name, int age) {this->name=new Char[strlen (name)];    strcpy (This->name,name); This->age=age;}    Teacher::teacher (const Teacher &teacher) {this->name=new Char[strlen (teacher.name)];    strcpy (This->name,teacher.name); This->age=age;}        Teacher::~teacher () {if (this->name!=null) {delete[] this->name;        this->name=null;    age==0; }}void Teacher::p rintteacher () {cout<< "Teacher (name:" <<this->name<< "Age:" <<this-> age<< ")" &LT;&LT;ENDL;} Ostream &operator << (ostream &out,teacher &teacher) {out<< "Teacher (name:" <<    teacher.name<< "Age:" <<teacher.age<< ")" <<endl; return out;} Teacher & teacher::operator = (const Teacher &teacher) {if (this->name ! = NULL) {delete[] this->name;    this->name=null;    } this->name=new Char[strlen (Teacher.name)];    strcpy (This->name,teacher.name);    this->age=teacher.age; return *this;}

3. Test function

Main function

#include "myvector.cpp" #include "Teacher.h" int main () {    myvector<int> myv;    Myv.init (ten);    for (int i = 0; i < Myv.getlen (); ++i) {        myv[i] = i+1;        cout<<myv[i]<< "";    }    cout<<endl;    Myvector<int> Myv2=myv;    for (int i = 0; i < Myv2.getlen (); ++i) {        cout<<myv2[i]<< "";    }    cout<<endl;    Myvector<int> Myv3;    MYV3=MYV;    for (int i = 0; i < Myv3.getlen (); ++i) {        cout<<myv3[i]<< "";    }    Teacher T1 ("GGG"), T2 ("QQQ", at $), T3 ("xxx", +);    Myvector<teacher> T;    T.init (3);    T[0]=T1;    T[1]=t2;    T[2]=T3;    cout<<endl;    for (int i = 0; i < 3; ++i) {        cout<<t[i];    }    return 0;}

Precautions:

In the main function, add the

#include "Myvector.cpp"

The root of the problem is the compiler's process of compiling the template

This is roughly the case? ):

1, Template Myvector in the compilation (compile) did not generate specific binary code,

There is no code to embed this function in the main function, it may contain just one sentence

Call TestFunc and the like (details later)


2, compile phase, in the main function found the myvector reference, but there is no correlation in main.obj

Executable code (the compiler thinks the function is defined elsewhere, which is why a link is required

Link, Although reference to Myvector in main but only provides a call virtual address without

The actual execution code )


3. Link stage, organize the modules (many *.obj files generated during compilation)

The image is that, in the link when the TestFunc "embed" in, like a sub-process,

Jump from the call to here in Main, execute and then step out of the submodule, from "Break Point"

Continue execution of subsequent statements)


4. The template does not generate specific code during compilation.

Application of C + + templates

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.