一、作用:
- 使复杂函数的理解更直观,程序更加简单易懂
二、运算符重载函数的形式是:
返回类型 operator 运算符符号(参数说明)
{
//函数体的内部实现
}
void operator +(Test&, Text&)
{
//函数体的内部实现
}
- 至少有一个参数是自定义类型(数组,类)
- 如果是单目运算符,只传一个参数
- 如果是双目运算符,传两个参数
* 三、C++也规定了一些运算符不能够自定义重载
- 例如: “::”、“*”、“.”、“?:”。 即作用域、取值、取成员、三目运算符
四、常见运算符重载函数的使用
注:C++会隐藏第一个参数,转而取代的是一个this指针
4.1 一种是作为类的友元函数进行使用
如:
friend void operator + (Test&, Test&);
具体例子:
.h头文件
#ifndef __OPERATOR_OVERWRITE_H__
#define __OPERATOR_OVERWRITE_H__
class MyString
{
private:
char *m_str;
public:
MyString(char *m_str);
~MyString();
void print();
//friend void add(MyString &ob1, MyString &ob2);
friend void operator + (MyString &ob1, MyString &ob2);
};
extern void operator + (MyString &ob1, MyString &ob2);
//extern void add(MyString &ob1, MyString &ob2);
#endif
operator_overwrite.cpp文件
#include "operator_overwrite.h"
#include <iostream>
#include <string>
using namespace std;
MyString::MyString(char *str)
{
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
cout << "构造函数" << endl;
}
MyString::~MyString()
{
delete [] m_str;
cout << "析构MyString" << endl;
}
void MyString::print()
{
cout << "m_str = " << m_str << endl;
}
void operator + (MyString &ob1, MyString &ob2)
//void add(MyString &ob1, MyString &ob2)
{
char *p = new char[strlen(ob1.m_str) + 1];
strcpy(p, ob1.m_str);
delete[] ob1.m_str; //释放旧的存储空间
ob1.m_str = p;
strcat(ob1.m_str, ob2.m_str);
}
main.cpp文件
#include "operator_overwrite.h"
#include <iostream>
using namespace std;
int main()
{
MyString str1((char *)"hehe");
MyString str2((char *)"haha");
str1.print();
str2.print();
//第一种表示方法:add(str1, str2);
//第二种表示方法:operator + (str1, str2); //运算符重载的原型
//第三种表示方法:
str1 + str2;
str1.print();
getchar();
return 0;
}
4.2 一种则是作为类的成员函数进行使用
避免了重新定义额外的函数,如:
Temp operator + (Temp &t)
{
...;
}
具体例子:
operator_overwrite1.h文件
#ifndef __OPERATOR_OVERWRITE1_H__
#define __OPERATOR_OVERWRITE1_H__
class MyString
{
private:
char *m_str;
public:
MyString(char *str);
~MyString();
void print();
void operator + (MyString &ob);//运算符重载函数
};
#endif
operator_overwrite1.cpp文件
#include "operator_overwrite1.h"
#include <iostream>
using namespace std;
MyString::MyString(char *str)
{
cout << "构造函数" << endl;
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
}
MyString::~MyString()
{
cout << "析构函数" << endl;
delete[] m_str;
}
void MyString::print()
{
cout << "m_str = " << m_str << endl;
}
void MyString::operator + (MyString &ob)
{
char *p = new char[strlen(this->m_str) + strlen(ob.m_str) + 1];
strcpy(p, this->m_str);
delete[] this->m_str;//释放this->str指向的旧空间
this->m_str = p;
strcat(this->m_str, ob.m_str);
}
main.cpp文件
#include "operator_overwrite1.h"
#include <iostream>
using namespace std;
int main()
{
{
MyString aa("你好");
MyString bb("明天!");
aa.print();
bb.print();
//aa.operator+(bb);//运算符重载原型
aa + bb;
aa.print();
}
getchar();
return 0;
}
注:
- 问题1、如果再定义一个新对象cc,将bb的值直接赋给cc(旧对象赋值给新对象),即
MyString cc = bb;
则需要再添加一个拷贝构造函数
MyString(const MyString &ob)
{
cout << "拷贝构造函数" << endl;
str = new char[strlen(ob.str)+1];
strcpy(str, ob.str);
}
- 问题2、旧对象给旧对象赋值时
解决办法:可通过等号运算符重载
void MyString::operator = (MyString &ob)
{
delete[] m_str;//释放this->str指向的旧空间
m_str = new char[strlen(m_str) + 1];
strcpy(m_str, ob.m_str);
cout << "=重载" << endl;
}
在main函数中加上重载原型:
MyString dd;
//dd.operator = (cc); //操作原型
dd = cc;
dd.print();