C\C++——重載操作符(operator)

運算符重載的限制

可以重載的運算符

+     -     *     /     %     ^     &     |     ~

!     =     <     >     +=    -=    *=    /=    %=

^=    &=    |=    <<    >>    >>=   <<=   ==    !=

<=    >=    &&    ||    ++    --    ->*    ‘    ->

[]    ()    new   delete     new[]     delete[]

不能重載的運算符

.	::	 .*	 ?:	 sizeof

注:重載運算符函數可以對運算符做出新的解釋,但原有基本語義不變

運算符函數是一種特殊的成員函數或友元函數(運算符重載的本質是一個函數

類型 類名 :: operator  op(參數表){}
返回值       關鍵字    函數名(操作數) 

全局函數、類成員函數方法實現運算符重載步驟

1.要承認操作符重載是一個函數,寫出函數名稱operator op ()

2.根據操作數,寫出函數參數

3.根據業務,完善函數返回值(看函數是返回引用 還是指針 元素),及實現函數業務

例:字符串類的封裝

// MyString.h

#pragma once
#include <iostream>
using namespace std;
#pragma warning(disable : 4996)

class MyString
{
	friend ostream & operator<<(ostream &out, MyString &s);
	friend istream & operator>>(istream &input, MyString &s);
public:
	MyString(const char *s );
	MyString(int len = 0);
	MyString(const MyString &obj);
	void MyPrint();
	~MyString();
public:
	MyString & operator=(const MyString &obj);
	MyString & operator=(const char *s);
	char &operator[](int index)const;

public:
	bool operator==(const char *str)const;
	bool operator!=( const char *str)const;
	bool operator==(const MyString &s)const;
	bool operator!=(const MyString &s)const;

	/*bool operator<(const char *str)const;
	bool operator>(const char *str)const;
	bool operator<(const MyString &s)const;
	bool operator>(const MyString &s)const;*/

public:
	char * Str();
	const char *c_Str();
	int Length();
	

private:
	char *str;
	int m_len;
};

//MyString.cpp

#include "MyString.h"

MyString::MyString(const char *s)
{
	if (s == NULL)
	{
		this->m_len = 0;
		this->str = new char[m_len + 1];
		strcpy(str, "");
	}
	else
	{
		this->m_len = strlen(s);
		cout << m_len << endl;
		this->str = new char[m_len + 1];
		strcpy(str, s);
	}
}

MyString::MyString(int len)
{
	if (len == 0)
	{
		this->m_len = 0;
		this->str = new char[m_len + 1];
		strcpy(str, "");
	}
	else
	{
		this->m_len = len;
		this->str = new char[m_len + 1];
		memset(this->str, 0, m_len);
	}
}

MyString::MyString(const MyString & obj)
{
	this->m_len = obj.m_len;
	this->str = new char[m_len + 1]; 
	strcpy(str, obj.str);
}

void MyString::MyPrint()
{
	cout << str << endl;
}


MyString::~MyString()
{
	if (this->str != NULL)
		delete[] str;
	this->m_len = 0;
}

MyString & MyString::operator=(const MyString &obj)
{
	if (this->str != NULL)
	{
		delete[] str;
		m_len = 0;
	}
	this->m_len = obj.m_len;
	this->str = new char[m_len+1];
	strcpy(str, obj.str);
	return *this;
}

MyString& MyString::operator=( const char *s)
{
	if (this->str != NULL)
	{
		delete[] str;
		m_len = 0;
	}
	if (s == NULL)
	{
		m_len = 0;
		str = new char[m_len + 1];
		strcpy(str, "");
	}
	else
	{
		m_len = strlen(s);
		str = new char[m_len + 1];
		strcpy(str, s);
	}
	return *this;
}

char & MyString::operator[](int index) const
{
	return str[index];
}

bool MyString::operator==(const char * str)const
{
	if (str == NULL)
	{
		if (this->m_len == 0)
			return true;
		else
			return false;
	}
	else
	{
		if (this->m_len == strlen(str))
		{
			 return !strcmp(this->str, str);
		}
		else
		{
			return false;
		}
	}
}

bool MyString::operator!=(const char * str)const
{
	return !(*this == str);
}

bool MyString::operator==(const MyString & s)const
{
	if (m_len != s.m_len)
		return false;
	else
	{
		return !strcmp(this->str, s.str);
	}
}

bool MyString::operator!=(const MyString & s)const
{
	return !(*this == s);
}

char * MyString::Str()
{
	return this->str;
}

const char * MyString::c_Str()
{
	return this->str;
}

int MyString::Length()
{
	return this->m_len;
}

//bool MyString::operator<(const char * str) const
//{
//	return strcmp(this->str, str );
//}
//
//bool MyString::operator>(const char * str) const
//{
//	return strcmp(str, this->str);
//}
//
//bool MyString::operator<(const MyString & s) const
//{
//	return strcmp(this->str, s.str);
//}
//
//bool MyString::operator>(const MyString & s) const
//{
//	return strcmp(s.str, this->str);
//}

ostream & operator<<(ostream & out, MyString & s)
{
	return out << s.str;
}

istream & operator>>(istream & input, MyString & s)
{
	return input >> s.str;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章