实现非常简单的数组模板类(MyVector)中用到了泛型编程(模板类编程)、运算符重载、友元函数等知识,最重要的是加深了对内存分配的理解。
所有容器提供的都是值(value)语意,而非引用(reference)语意。容器执行插入元素的操作时,内部实施拷贝动作。所以STL容器内存储的元素必须能够被拷贝(必须提供拷贝构造函数)。
开始的时候不理解这句话,然后自己敲代码就理解了。我们在往容器里存数据的时候,是进行拷贝动作,也就是说将外部变量的值拷贝给容器中的值。要进行拷贝就必须分配内存,没有分配内存的话往哪拷数据呢????而基础数据类型的变量是不用担心这些问题的,因为当我们写下int a
时编译器已经为a
分配了内存,但是如果是指针变量就必须考虑深拷贝与浅拷贝的问题(其实这个地方第一次的时候我想的是,MyVector不是已经分配了内存了吗,为什么还要分配呢?其实在MyVector中是给类分配了内存说白了就是给类中的成员变量分配了内存,而成员变量若有指针的话,它只是为指针分配了内存,而我们所需要的是分配指针所指向的内存空间分配内存)。
MyVector.h
#pragma once
#include<iostream>
using namespace std;
template <typename Type>
class MyVector
{
friend ostream& operator<< <Type> (ostream &out, const MyVector &obj);
//在泛型编程当中,类模板中避免使用友元函数,除了重载 "<<" ">>" 左移右移运输算符,其他的函数都写成成 //员函数
public:
MyVector(int len);
MyVector(const MyVector &obj);
~MyVector();
public:
MyVector& operator=(const MyVector &obj);
Type& operator[](int index);
private:
Type *mSpace;
int len;
};
MyVector.cpp
#include<iostream>
using namespace std;
#include"MyVector.h"
template <typename Type>
MyVector<Type>::MyVector(int len)
{
this->len = len;
mSpace = new Type[len];
}
template <typename Type>
MyVector<Type>::MyVector(const MyVector &obj)
{
this->len = obj.len;
mSpace = new Type[len];
for (int i = 0; i < len; i++)
{
mSpace[i] = obj.mSpace[i];
}
}
template <typename Type>
MyVector<Type>::~MyVector()
{
if (mSpace != NULL)
{
delete[] mSpace;
mSpace = NULL;
len = 0;
}
}
template <typename Type>
MyVector<Type>& MyVector<Type>::operator=(const MyVector<Type> &obj)
{
if (mSpace != NULL)
{
delete[] mSpace;
mSpace = NULL;
len = 0;
}
this->len = obj.len;
mSpace = new Type[len];
for (int i = 0; i < len; i++)
{
mSpace[i] = obj.mSpace[i];
}
return *this;
}
template <typename Type>
Type& MyVector<Type>::operator[](int index)
{
return mSpace[index];
}
template <typename Type>
ostream& operator<<(ostream &out, const MyVector<Type> &obj)
{
for (int i = 0; i < obj.len; i++)
{
cout << obj.mSpace[i];
}
cout << endl;
return out;
}
main.cpp
测试int
型
int main()
{
MyVector<int> arr1(10);
for (int i = 0; i < 10; i++)
{
arr1[i] = i + 1;
}
cout << arr1 << endl;
MyVector<int> arr2 = arr1;
cout << arr2 << endl;
return 0;
}
测试char
型
int main02()
{
MyVector<char> str1(10);
for (int i = 0; i < 10; i++)
{
str1[i] = 'a' + i;
}
cout << str1 << endl;
MyVector<char> str2 = str1;
cout << str2 << endl;
return 0;
}
测试Teacher
类
//Teacher类中名字是字符数组,即在栈上分配内存。
class Teacher
{
friend ostream& operator<<(ostream& out, Teacher &obj);
public:
Teacher()
{
age = 0;
strcpy(name, "");
}
Teacher(const char *name,int age)
{
strcpy(this->name, name);
this->age = age;
}
void printT()
{
cout << "name:" << name << " age:" << age << endl;
}
//栈上分配的内存还用我去释放? 我傻了 -_-
//~Teacher()
//{
// if (name != NULL)
// {
// delete name;
// name = NULL;
// age = 0;
// }
//}
private:
char name[16];
char *name;
int age;
};
ostream& operator<<(ostream& out, Teacher &obj)
{
out << obj.name << " " << obj.age << endl;
return out;
}
//Teacher中名字是一个指针,即需要我们手动分配内存否则会出现程序宕掉的情况。
class Teacher
{
friend ostream& operator<<(ostream &out, Teacher &obj)
{
out << "name:" << obj.name << " age:" << obj.age << endl;
return out;
}
public:
//这样的话可以不写无参构造函数吗 经测试可以 那还要无参构造函数干嘛呢???
Teacher(const char *name = "", int age = 0)
{
//这个地方要吗?
this->age = age;
this->name = new char[strlen(name) + 1];//还有\0
strcpy(this->name, name);
}
Teacher(const Teacher &obj)
{
this->age = obj.age;
name = new char[strlen(obj.name) + 1];
strcpy(name, obj.name);
}
~Teacher()
{
if (name != NULL)
{
delete[] name;
name = NULL;
age = 0;
}
}
public:
Teacher& operator=(const Teacher &obj)
{
this->age = obj.age;
this->name = new char[strlen(obj.name) + 1];
strcpy(name, obj.name);
return *this;
}
void printT()
{
cout << "name:" << name << " age:" << age << endl;
}
private:
char *name;
int age;
};
int main()
{
MyVector<Teacher> tArray(3);
Teacher t1("123", 30);
Teacher t2("456", 31);
Teacher t3("789", 32);
tArray[0] = t1;
tArray[1] = t2;
tArray[2] = t3;
Teacher t4;
for (int i = 0; i < 3; i++)
{
Teacher tmp = tArray[i];
tmp.printT();
}
cout << endl;
cout << endl;
cout << endl;
cout << endl;
cout << tArray << endl;
return 0;
}
来源:CSDN
作者:optimjie
链接:https://blog.csdn.net/optimjie/article/details/103602836