需求:类比数组类,只不过数组类型不再是整型、浮点型等,也可以是类。

1、创建模板类

头文件

#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);   //构造函数
    myVector(const myVector &obj);  //拷贝构造函数
    ~myVector();    //析构函数
    const int getLen() const;
    const T* getSpace() const {return m_space;}
    T& operator [](int index);
    myVector<T> &operator =(const myVector<T> &obj);

private:
    T *m_space;
    int m_len;
};

#endif // MYVECTOR_H

资源文件

#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)  //拷贝构造函数
{
    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()    //析构函数
{
    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)
{
    //先释放旧的内存
    if(m_space != NULL){
        cout<<"not null"<<endl;
        delete[] m_space;
        m_space=NULL;
        m_len=0;
    }
    //根据参数分配内存
    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、实验类

头文件

#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);
private:
    int age;
    char *name;
};

#endif // TEACHER_H

资源文件

#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::printTeacher(){
    cout<<"Teacher( name: "<<this->name<<" age: "<<this->age<<" )"<<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、测试函数

主函数

#include "myvector.cpp"
#include"Teacher.h"

int main(){
    myVector<int> myv;
    myv.init(10);
    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",22),t2("qqq",23),t3("xxx",24);
    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;
}

注意事项:

  在主函数中加入了

#include "myvector.cpp"

问题的根源在于编译器对于模板(template)的编译处理过程中,

大致是这样的(果真如此么?):

1、模板myVector在编译(compile)期间并未生成具体二进制代码,

在main函数中也没有嵌入这个函数的代码,可能只是包含了一句

call testFunc之类的(稍后详述)

2、编译阶段,在main函数中发现了myVector的引用,但是main.obj中没有相关的

可执行代码(编译器认为该函数在别处定义,这就是为什么需要链接也就是

LINK了,在main中虽然引用到myVector但是只提供了一个call虚拟地址而没有

实际的执行代码)

3、链接阶段,将各个模块(编译期间生成的很多*.obj文件)组织起来

形象的说就是,在LINK的时候把testFunc“嵌入”进来,就像是一个子过程,

在main中从调用处jump到这里即可,执行完毕再跳出子模块,从“中断点”

继续执行后续语句)

4、模板在编译期间是不生成具体代码的。

最新文章

  1. cache-coherent nonuniform memory access
  2. 【新手】python爬虫遍历贴吧用户
  3. node+mongodb+ionic+cordova
  4. struts采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架
  5. jsp中的forward和redirect的区别
  6. ZOJ 2760 How Many Shortest Path(Dijistra + ISAP 最大流)
  7. c++设计成员变量可动态调整的动态类结构
  8. MyBatis之简单了解Plugin
  9. python logging一个通用的使用模板
  10. vultr测速 看看vultr哪个地区节点速度快
  11. go的基本数据类型
  12. CLR Via第一 章 知识点整理(4) FCL、CTS、CLI和CLS
  13. MySQL字符集及校对规则的理解
  14. create_buffer_tree example
  15. windows下elasticsearch启动
  16. USB2.0学习笔记连载(十):关于WIN8及以上系统哈希值问题
  17. mysql数据库字符集初步理解
  18. DOM编程 学习笔记(二)
  19. MinGW和MSYS的自动安装 【转】
  20. tcpdf中文解决方案

热门文章

  1. Linux驱动之异常处理体系结构简析
  2. 设置textfield 文字左边距
  3. Zookeeper系列2 原生API 以及核心特性watcher
  4. 1-JRE与JDK等知识
  5. TensorFlow数据读取
  6. python基础循环
  7. w3wp.exe(IIS ) CPU 占用 100% 的常见原因
  8. 【对比分析四】position的absolute与fixed共同点与不同点
  9. ubuntu 配置jdk报错解决办法
  10. GIS中空间数据和非空间数据