关于运算符重载的各种应用,具体看代码注释
MyString.h
#pragma once
#include<iostream>
using namespace std;
//c中没有字符串,字符串类(c风格的字符串)
//空串 ""
class MyString
{
friend ostream& operator<<(ostream &out,MyString &s); //这里是函数运算符重载,之所以用全局而不是用成员函数,是因为第一个操作数无法通过this指针隐士传递,第一个操作数其类型是ostream
friend istream& operator>>(istream& in,MyString &s);
public:
MyString(int len = 0);
MyString(const char* p);
MyString(const MyString& s);
~MyString();
public: //重载=号运算符
MyString& operator=(const char* p); //第一个操作数通过this指针隐式传递
MyString& operator=(const MyString& s);
char& operator[](int index);
public: //重载 == !==
bool operator==(const char *p) const;
bool operator==(const MyString &s) const;
bool operator!=(const char *p) const;
bool operator!=(const MyString& s) const;
public:
int op