编译器给提供了一种机制,让用户自己去完成自定义类型的加减操作,这个机制就是运算符重载
运算符重载的本质是一个函数!函数!函数!
语法为:类型 类名::operatro op(参数)
写的时候先写operator,然后在写后边的操作,比方说<,> ,= ,!=,==,<<,>>这样,之后写()里的内容,比方说你对运算符+号进行重载,然后你可以是一个类的对象,也可以是一个属性,即里边可以是(&s)或者(*p)这样,然后在写前边的类型,这个类型看的是你要返回的类型,最后才是写类名::
小练习:MyString类,关于运算符的重载
MyString.h
#include<iostream>
using namespace std;
class MyString{
//重载<<操作符
friend ostream& operator<<(ostream &out,MyString &s);
friend istream& operator>>(istream &in,MyString &s);
public:
MyString();
MyString(int len);
MyString(const char * p);
MyString(const MyString &s);
~MyString();
//重载=操作
MyString& operator=(const char *p);
MyString& operator=(const MyString &s);
char& operator[] (int index);
//重载== !==
bool operator==(const char*p);
bool operator==(const MyString &s);
bool operator!=(const char*p);
bool operator!=(const MyString &s);
//重载>和<操作
int operator>(const char*p);
int operator>(const MyString &s);
int operator<(const char*p);
int operator<(const MyString &s);
//把类的指针露出来
const *c_str()
{
return m_p;
}
const char *c_str2(){
return m_p;
}
int length(){
return m_len;
}
private:
int m_len;
char *m_p;
};
MyString.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"MyString.h"
MyString::MyString()
{
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p,"");
}
MyString::MyString(int len)
{
if(len == 0)
{
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p,"");
}
else
{
m_len=len;
m_p=new char[m_len+1];
//strcpy(m_p,"");
memset(m_p,0,m_len);
}
}
MyString::MyString(const char*p)
{
if(p == NULL)
{
m_len=0;
m_p=new char[m_len+1];
strcpy(m_p,"");
}
else
{
m_len=strlen(p);
m_p=new char[m_len+1];
strcpy(m_p,p);
}
}
//拷贝构造函数
MyString::MyString(const MyString& s)
{
m_len = s.m_len;
m_p = new char[m_len+1];
strcpy(m_p,s.m_p);
}
MyString::~MyString()
{
if(m_p != NULL)
{
delete [] m_p;
m_p = NULL;
m_len = 0;
}
}
MyString& MyString::operator=(const char *p)
{
//释放旧内存
if(m_p != NULL)
{
delete [] m_p;
m_len = 0;
}
//根据p分配内存
if(p == NULL)
{
m_len=0;
m_p = new char[m_len+1];
strcpy(m_p,"");
}
else
{
m_len=strlen(p);
m_p = new char[m_len+1];
strcpy(m_p,p);
}
return *this;
}
MyString& MyString::operator=(const MyString &s)
{
//释放旧内存
if(s.m_p != NULL)
{
delete [] m_p;
m_len = 0;
}
//根据s分配内存
m_len=s.m_len;
m_p = new char[m_len+1];
strcpy(m_p,s.m_p);
return *this;
}
char& MyString::operator[] (int index)
{
return m_p[index];
}
ostream& operator<<(ostream &out,MyString &s)
{
out<<s.m_p;
return out;
}
istream& operator>>(istream &in,MyString &s)
{
in>>s.m_p;
return in;
}
bool MyString::operator==(const char*p)
{
if(p == NULL)
{
if(m_len == 0)
{
return true;
}
else
{
return false;
}
}
if(m_len == strlen(p))
{
return !strcmp(m_p,p);
}
else
{
return false;
}
}
bool MyString::operator==(const MyString &s)
{
if(m_len!=s.m_len)
{
return false;
}
else
{
return !strcmp(s.m_p,m_p);
}
}
bool MyString::operator!=(const char*p)
{
return !(*this ==m_p);
}
bool MyString::operator!=(const MyString &s)
{
return !(s.m_p ==m_p);
}
//重载>和<操作
int MyString::operator>(const char*p)
{
return strcmp(this->m_p,p);
}
int MyString::operator>(const MyString &s)
{
return strcmp(s.m_p,m_p);
}
int MyString::operator<(const char*p)
{
return strcmp(p,this->m_p);
}
int MyString::operator<(const MyString &s)
{
return strcmp(m_p,s.m_p);
}
MyString_Test.cpp//里边并没有把所有重载运算符都用上
#include<iostream>
#include"MyString.h"
using namespace std;
int main(){
MyString s1;
MyString s2("s2");
MyString s3=s2;
MyString s4="s444444444";
//测试运算符重载 和 重载[]
//重载等号操作符
s4=s2;
s4="s222222";
//MyString& operator=(const char *p);//s4="s222"
//MyString& operator=(const MyString &s);//s4=s2
system("pause");
return 0;
}