运算符重载
重载本质上是函数调用,并且一个运算符在同一个类中只能被重载一次
- = () -> [] 只能采用成员函数的方式重载
- . .* ?: ::这些不能被重载
- 习惯: 单目运算符采用类成员函数方式重载,双目运算符采用友元函数方式实现
有如下分类:
-
流重载: 只能采用友元函数方式重载 流重载函数返回值必须是引用
输入流重载: 例子: string name; cin>>name; 分支cin istream类的对象
输出流重载:例子: string name; cout<<name; 分支cout ostream类的对象 -
特殊运算符重载
++ --重载 (要区分前置和后置)
对于前置和后置的区分
opeartor++(int) //这里是后置++ 这里函数int是一个占位符 用于区分前置或者后置
opeartor++() //这里是前置++文本重载(重载后缀)
参数一定是unsigned long long
一般做下划线的系列和库中做一个区分
unsigned long long operator""_h(unsigned long long num)
{
return num * 60 * 60;
}
void printSec(int num)
{
cout << num << endl;
}
调用:
printSec(1_h);
3.隐式转换
重载()
//这里重载了() 函数调用特殊运算符
class Object {
public:
bool operator()(int i, int j)
{
cout << "operator()被调用" << endl;
return i > j;
}
};
调用:
Object object;
object(1, 2); //第一种方式用对象
Object()(1, 2); //第二种方式用类名
自己实现的string类
#include <iostream>
#include <string>
using namespace std;
class MyString {
public:
MyString()
{
cout << "MyString无参构造函数" << endl;
}
MyString(const char* str)
{
cout << "调用构造函数" << endl;
cout << "strlen(str)=" << strlen(str) << endl;
string = new char[strlen(str) + 1];
strcpy_s(string, strlen(str) + 1, str);
this->m_size = strlen(str);
}
MyString(const MyString& myStr)
{
cout << "调用拷贝构造函数" << endl;
if (this->string != NULL)
{
delete[] this->string;
this->string = NULL;
}
string = new char[strlen(myStr.string) + 1];
strcpy_s(this->string, strlen(myStr.string) + 1, myStr.string);
this->m_size = strlen(myStr.string);
}
MyString& operator=(const MyString& myStr)
{
if (this->string != NULL)
{
delete[] this->string;
this->string = NULL;
}
string = new char[strlen(myStr.string) + 1];
strcpy_s(this->string, strlen(myStr.string) + 1, myStr.string);
this->m_size = strlen(myStr.string);
return *this;
}
MyString operator+(MyString& myStr)
{
int newSize = this->m_size + myStr.m_size + 1;
char *temp= new char[newSize];
memset(temp, 0, newSize);
strcat(temp, this->string);
strcat(temp, myStr.string);
MyString tempString(temp);
delete[] temp;
return tempString;
}
MyString operator+(const char *str)
{
int newSize = this->m_size + strlen(str) + 1;
char* temp = new char[newSize];
memset(temp, 0, newSize);
strcat(temp, this->string);
strcat(temp, str);
MyString tempString(temp);
if (temp)
{
delete[] temp;
}
return tempString;
}
bool operator==(const char*str)
{
if (!strcmp(this->string, str) && this->m_size == strlen(str))
{
return true;
}
else
return false;
}
bool operator==(MyString &myStr)
{
if (!strcmp(this->string, myStr.string) && this->m_size == strlen(myStr.string))
{
return true;
}
else
return false;
}
~MyString()
{
//cout << "调用析构函数" << endl;
if (this->string != NULL)
{
delete[] this->string;
this->string = NULL;
}
}
void print()
{
cout << this->string << endl;
}
friend ostream& operator<<(ostream& cout, MyString& str);
friend istream& operator>>(istream& cin, MyString& str);
protected:
char* string;
int m_size; //字符长度
};
ostream& operator<<(ostream& cout, MyString& str)
{
if (!str.string)
{
cout << "";
}
else {
cout << str.string;
}
return cout;
}
istream& operator>>(istream& cin, MyString& str)
{
if (str.string != NULL)
{
delete[] str.string;
str.string = NULL;
}
char temp[10];
cin >> temp;
str.string = new char[strlen(temp) + 1];
strcpy_s(str.string, strlen(temp) + 1, temp);
str.m_size = strlen(temp);
return cin;
}
int main()
{
char str[] = "hello";
MyString myStr("hello");
//myStr.print();
MyString myStr2(myStr);
//myStr2.print();
cout << myStr<<"\t" << myStr2<<endl;
cout << "mystr3:" << endl;
MyString myStr3;
cout << myStr3 << endl;
cout << "输入:" << endl;
//cin >> myStr3;
myStr3 = myStr2;
cout << myStr3 << endl;
cout << "使用+:" << endl;
MyString str1("str1+");
MyString str2("str2");
cout << str1 << endl;
cout << str2 << endl;
MyString str3 = str1 + str2;
cout << str3 << endl;
if (str3 == str2)
{
cout << "str3和str2相等" << endl;
}else
cout << "str3和str2不相等" << endl;
return 0;
}