#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
public:
MyString():pstring(NULL),m_size(0)
{}
MyString(const char *str);
MyString(const MyString &obj);
void show();
char &operator[](int index);
void operator=(const char *obj);
MyString& operator=(const MyString&obj);
MyString& operator+(const char *obj);
MyString& operator+(const MyString &obj);
bool operator==(const MyString &obj);
private:
char *pstring;
int m_size;
};
bool MyString::operator==(const MyString &obj)
{
if(this->m_size == obj.m_size)
{
int i=0;
while(this->pstring[i] != '\0')
{
if(pstring[i] != obj.pstring[i])
{
return false;
}
}
return true;
}
else
return false;
}
MyString& MyString::operator+(const MyString &obj)
{
char *temp = NULL;
int num = 0;
if(this->pstring != NULL)
{
temp = new char(m_size);
num = m_size-1;
strcpy(temp, pstring);
delete pstring;
}
m_size = obj.m_size;
m_size +=num;
pstring = new char[m_size];
strncpy(pstring, temp,num);
strncpy(pstring+num,obj.pstring,obj.m_size);
return *this;
}
MyString& MyString::operator+(const char* obj)
{
char *temp = NULL;
int num = 0;
if(this->pstring != NULL)
{
temp = new char(m_size);
num = m_size-1;
strcpy(temp, pstring);
delete pstring;
}
m_size = strlen(obj)+1;
m_size +=num;
pstring = new char[m_size];
strncpy(pstring, temp,num);
strncpy(pstring+num,obj,m_size-num);
return *this;
}
MyString& MyString::operator=(const MyString&obj)
{
if(this == &obj)
{
return *this;
}
if(this->pstring != NULL)
{
delete pstring;
}
m_size = strlen(obj.pstring) + 1;
pstring = new char[m_size];
strcpy(pstring, obj.pstring);
return *this;
}
void MyString::operator=(const char *obj)
{
delete pstring;
m_size = strlen(obj) + 1;
pstring = new char[m_size];
strcpy(pstring, obj);
}
char& MyString::operator[](int index)
{
return pstring[index];
}
MyString::MyString(const char *str)
{
m_size = strlen(str) + 1;
pstring = new char[m_size];
strcpy(pstring, str);
}
MyString::MyString(const MyString &obj)
{
m_size = strlen(obj.pstring) + 1;
pstring = new char[m_size];
strcpy(pstring, obj.pstring);
}
void MyString::show()
{
cout << pstring << " m_size = " << m_size << endl;
}
int main(int argc, char const *argv[])
{
MyString m1("hello word");
m1.show();
MyString m2(m1);
m2.show();
cout << m2[3] << endl;
m2 = "nihao beijing";
m2.show();
MyString m3;
m3 = m2 = m1;
// m3.operator=(m2.operator=(m1));
m1.show();
m2.show();
m3.show();
cout << "--------" << endl;
MyString m4("nihao ");
m4 = m4 + "helloworld";
m4.show();
MyString m5("hahahaha");
m5.show();
m5 = m5 + m4;
m5.show();
if(m5 == m4)
{
cout << "m5 == m4" << endl;
}
else
{
cout << "m5 != m4" << endl;
}
return 0;
}
函数模板
通过建立一个通用的函数,其函数类型与形参类型不具体指定,用一个虚拟类型表示,被称为函数模板。
template <typename T>
类型 函数名(T 形参名)
{
}
函数模板实现机制
二次编译/延迟编译:
1.编译时看到函数模板定义,只检查语法错误,不检查数据类型的错误。
2.当实例化对象时,看到模板函数,根据传递的模板实参类型检查类型错误。
这样的编译方式被称为二次编译。
#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << a << " " << b << endl;
}
template <typename T>
void add(T a, T b)
{
cout << a << " " << b << endl;
}
template <typename T1,typename T2>
void print(T1 a, T2 b)
{
cout << a << " " << b << endl;
}
int main(int argc, char * argv[])
{
add(1,0);
print('a',5);
return 0;
}