C++重载运算符小结与注意点

Posted Wecccccccc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++重载运算符小结与注意点相关的知识,希望对你有一定的参考价值。

重载运算符需注意:

1.重载=运算符时容易忘记写返回值。

2.重载赋值运算符时,记得加const,因为赋值操作必须是固定的右值。

3重载时,写在类中的只能有一个参数(实际有两个参数,另外一个是this指针,我们看不见而已),需要两个参数的时候,要写在类外,用友元在类内声明。

4.重载递增运算符时,要注意哪个要加引用,哪个不用加引用。

//重载前置++运算符  返回引用是为了一直对一个数据进行递增操作
		MyInteger &operator++() {
			//先进行++运算
			m_Num++;
			//再将自身做一个返回
			return *this;
		}
		//重载后置++运算符
		//这个int代表的是占位参数,可以用于区分前置和后置递增
		MyInteger operator++(int) { //后置递增返回的是值,不能是引用
			//先 返回结果
			MyInteger temp = *this;
			//后 递增
			m_Num++;
			//最后将记录结果做返回
			return temp;

代码如下:

#include <iostream>
using namespace std;

class Base
{
public:

	Base():num(0){}

	Base(const Base &a)
	{
		this->num = a.num;
		int len = strlen(a.name);
		this->name = new char[len + 1];
		for (int i = 0; i < len; i++) this->name[i] = a.name[i];
		this->name[len] = '\\0';

	}

	~Base()
	{
		if (name != nullptr) delete[] name;
	}

	Base(const char *name, int num) :num(num)
	{
		int len = strlen(name);
		this->name = new char[len+1];
		for (int i = 0; i < len; i++) this->name[i] = name[i];
		this->name[len] = '\\0';
	}

	Base operator+(Base &a)
	{
		Base tmp;
		tmp.num = this->num + a.num;
		int len1 = strlen(this->name);
		int len2 = strlen(a.name);
		int len3 = len1 + len2;
		tmp.name = new char[len3 + 1];
		for (int i = 0; i < len1; i++) tmp.name[i] = this->name[i];
		for (int i = len1; i < len3; i++) tmp.name[i] =a.name[i-len1];
		tmp.name[len3] = '\\0';
		return tmp;
	}

	Base operator+( const int a)
	{
		Base tmp(*this);
		tmp.num += a;
		return tmp;
	}

	friend Base operator+(const int, Base&);


	friend ostream&operator<<(ostream &, const Base&);
	friend istream &operator>>(istream &, Base&);

	Base operator()(const char a[], int b)
	{
		if (name != nullptr) delete[] name;
		int len = strlen(a);
		name = new char[len + 1];
		for (int i = 0; i < len; i++) name[i] = a[i];
		name[len] = '\\0';
		num = b;
		return *this;
	}

	bool operator==(const Base &a)
	{
		if (name == a.name && num == a.num) return true;
		int len1 = strlen(a.name);
		int len2 = strlen(this->name);
		if (len1 == len2 && num == a.num)
		{
			for (int i = 0; i < len1; i++)
			{
				if (name[i] != a.name[i]) return false;
			}
			return true;
		}
		return false;
	}

	bool operator !=(const Base &a)
	{
		if (*this == a) return false;
		else return true;
	}

	Base &operator=(const Base &a)
	{
		if (this == &a) return *this;
		if (name != nullptr) delete[]  name;
		int len = strlen(a.name);
		name = new char[len + 1];
		for (int i = 0; i < len; i++) name[i] = a.name[i];
		name[len] = '\\0';
		num = a.num;
		return *this;
	}



	Base operator++(int)
	{
		Base tmp(*this);
		num++;
		return tmp;
	}

	Base &operator++()
	{
		num++;
		return *this;
	}

	void print()
	{
		cout << "name = " << name << endl;
		cout << "num = " << num << endl;
	}

private:
	int num;
	char *name;
};

ostream&operator<<(ostream &os,const  Base&p)
{
	os << "name = " << p.name << endl;
	os << "num = " << p.num << endl;
	return os;
}

istream &operator>>(istream &in, Base &p)
{
	char c[10000];
	in >> c;
	int len = strlen(c);
	p.name = new char[len + 1];
	for (int i = 0; i < len; i++) p.name[i] = c[i];
	p.name[len] = '\\0';
	in >> p.num;
	return in;
}


Base operator+(const int a, Base& b)
{
	Base tmp(b);
	b.num += a;
	return tmp;
}



int main()
{
	Base b("xiaom", 12);
	cout << b << endl;
	Base x("ADA", 32);
	x = x;
	Base c = b + x;
	cout << c << endl;
	cout<<x++<<endl;
	cout << ++x << endl;
	c = x;
	cout << c << endl;
	Base v = c + 32;
	int k = 3;
	Base x2 = c + k;
	Base x3 = k + c;
	cout << c << endl;
	cout << v << endl;
	v("hhh", 30);
	cout << v << endl;
	v = (c + 32);
	return 0;
}

以上是关于C++重载运算符小结与注意点的主要内容,如果未能解决你的问题,请参考以下文章

C++提高:运算符重载

C++提高:运算符重载

15. 函数运算符重载及转换的知识点小结

15. 函数运算符重载及转换的知识点小结

C++学习:5其他语法

类和对象—4