标签:生成 构造 虚拟 地址 释放 一个 ace let sof
需求:类比数组类,只不过数组类型不再是整型、浮点型等,也可以是类。
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、模板在编译期间是不生成具体代码的。
标签:生成 构造 虚拟 地址 释放 一个 ace let sof
原文地址:https://www.cnblogs.com/helloworldcode/p/9371234.html