最近刚开始学习模板编程,有些注意点
1.在类模板编程中对操作符<<进行重载时,类中声明时需要使用
friend ostream& operator<< <T>(ostream &out, const MyVector &obj);
2.所有容器提供的都是值(value)语意,而非引用(reference)语意。容器执行插入元素的操作时,内部实施拷贝动作。所以STL容器内存储的元素必须能够被拷贝(必须提供拷贝构造函数)
这个意思指在容器执行插入元素操作时,是把元素拷贝一份放在容器中,而不是引用对象。所以STL容器内存储的元素必须是能够被拷贝的(如果对象中存在指针类型的成员变量,则需要支持深拷贝)
下面是我写了一个简单的MyVector类模板,并且写了一个Teacher类,大家可以拷过去看看,应该会更加清楚
</pre><pre code_snippet_id="1562142" snippet_file_name="blog_20160122_1_5172763" name="code" class="html">
------------------myvector.h
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyVector
{
friend ostream& operator<< <T>(ostream &out, const MyVector &obj);
public:
MyVector(int size=0);
MyVector(const MyVector& obj);
~MyVector();
T& operator[](int index);
int GetVectorSize();
MyVector& operator=(const MyVector &obj);
private:
T *m_ptr;
int size;
};
------------------myvector.hpp
<span style="font-size:14px;"></span><pre name="code" class="cpp">#include<iostream>
#include"demo9.h"
#include"teacher.h"
using namespace std;
template<typename T>
ostream& operator<< (ostream &out, const MyVector<T> &obj)
{
for (size_t i = 0; i < obj.size; i++)
{
out << obj.m_ptr[i] << " ";
}
out << endl;
return out;
}
template<typename T>
MyVector<T>::MyVector(int size=0)
{
this->size = size;
m_ptr = new T[size];
}
template<typename T>
MyVector<T>::MyVector(const MyVector& obj)
{
this->size = obj.size;
this->m_ptr = new T[size];
for (size_t i = 0; i < this->size; i++) //不能用memcpy,如果T类型的复杂类型(包含指针)这需要支持深拷贝
{
this->m_ptr[i] = obj.m_ptr[i];
}
}
template<typename T>
MyVector<T>& MyVector<T>::operator=(const MyVector<T>& obj)
{
MyVector temp(obj);
this->size = temp.size;
swap(this->m_ptr, temp.m_ptr);
return *this;
}
template<typename T>
MyVector<T>::~MyVector()
{
if (m_ptr != NULL)
{
delete[] m_ptr;
m_ptr = NULL;
}
}
template<typename T>
T& MyVector<T>::operator[](int index)
{
return m_ptr[index];
}
template<typename T>
int MyVector<T>::GetVectorSize()
{
return size;
}
------------------teacher.h
<pre name="code" class="cpp">#pragma once
#include<iostream>
using namespace std;
class Teacher
{
friend ostream & operator<< (ostream & out, const Teacher &obj);
friend ostream & operator<<(ostream& out, const Teacher * obj);
public:
Teacher();
Teacher(const Teacher &obj);
Teacher& operator=(const Teacher &obj);
Teacher(char *name, int age);
void printT();
private:
int age;
char *pname;
};
------------------teacher.cpp
<pre name="code" class="cpp">#include<iostream>
#include"teacher.h"
#include<string.h>
using namespace std;
Teacher::Teacher()
{
age = 33;
pname = NULL;
}
Teacher::Teacher(char *name, int age)
{
this->age = age;
pname = new char[strlen(name) + 1];
strcpy_s(pname, strlen(name) + 1, name);
}
void Teacher::printT()
{
cout << pname << ", " << age << endl;
}
ostream& operator<<(ostream& out, const Teacher &obj)
{
return out << "age:" << obj.age << "name:" << obj.pname;
}
Teacher::Teacher(const Teacher &obj)
{
this->age = obj.age;
this->pname = new char[strlen(obj.pname) + 1];
strcpy_s(this->pname, strlen(obj.pname) + 1, obj.pname);
}
Teacher& Teacher::operator=(const Teacher &obj)
{
Teacher temp(obj);
this->age = temp.age;
swap(this->pname, temp.pname);
return *this;
}
ostream & operator<<(ostream& out, const Teacher * obj)
{
return cout << "age:" << obj->age << "name:" << obj->pname;
}
------------------main.cpp
#include<iostream>
#include"demo9.hpp"
#include"teacher.h"
void main()
{
Teacher t1("t1", 31), t2("t2", 32), t3("t3", 33), t4("t4", 34);
MyVector<Teacher> tArray(4);
tArray[0] = t1;
tArray[1] = t2;
tArray[2] = t3;
tArray[3] = t4;
MyVector<Teacher> *mArray = new MyVector<Teacher>;
*mArray = tArray;
MyVector<Teacher> nArray(*mArray);
for (int i = 0; i<4; i++)
{
Teacher tmp = tArray[i];
tmp.printT();
}
cout <<"tArray"<< tArray;
cout << "***************************" << endl;
cout << "mArray" << *mArray;
cout << "---------------------------" << endl;
cout << "nArray" << nArray;
delete mArray;
cout << "+++++++++++++++++++++++++++" << endl;
Teacher t5("xbb", 20), t6("sddf", 30), t7("dsd", 60);
MyVector<Teacher*> pArray(3);
pArray[0] = &t5;
pArray[1] = &t6;
pArray[2] = &t7;
cout << pArray << endl;
system("pause");
}