第一次写这种技术的博客,如果有大牛经过,请指导:
说到C++运算符重载,不得不先说说C++都有哪些运算符,C++包含以下运算符:
能重载的有:::::==(等于), !=(不等于), <(小于), >(大于),<=(小于等于),>=(大于等于), +(加),-(减),*(乘),/(除),%(取模),||(逻辑或),&&(逻辑与),!(逻辑非),+(正),-(负),*(指针),&(取地址),
++(自增),--(自减),|(按位或),&(按位与),~(按位取反),^(按位异或),<<(左移).>>(右移),=(等于),+=(加等于),-=(减等于),*=(乘等于),/=(除等于),%=(取模等于),&=(按位于等于),|=(按位或等于),
^=(按位异或等于),<<=(左移等于),>>=(右移等于),new,delete,new[],delete[] ,()(函数调用),->(成员访问),->*(成员指针访问),,(逗号),[](下标),>>(输入流),<<(输出流),
不能重载的五个:::::?:(条件运算符),.(成员访问运算符),.*(成员指针访问运算符),::(域运算符),sizeof(长度运算符)
还有一种比较特殊的可重载运算符:::强制类型转换运算符 格式为(类型名)(表达式)如int(a);
以下为各个运算符重载格式,代码没有任何意义,主要是注释里面的格式:
new,delete,new[],delete[] 的重载比较特殊,这里代码虽然有,但是我会在下一次blog更新的时候写new,delete,new[],delete[] 的详细说明.
重载为成员函数格式代码为:
/********************************************************************
filename: f:\work\testOperatorOverload\testOperatorOverload\MBString.h
file path: f:\work\testOperatorOverload\testOperatorOverload
file base: MBString
file ext: h
author: dyj
purpose: 学习重载运算符,且运算符作为成员函数
. .* :: sizeof ?: 不能被重载
本类实现的都是重载运算符作为成员函数
*********************************************************************/
#pragma once
#include <iostream>
using namespace std;
struct _structson_
{
int j;
};
struct _struct_//测试MBString类重载->使用
{
int i;
_structson_ *son;
_structson_* operator -> ()
{
return son;
};
};
class MBString
{
public:
MBString(void);
~MBString(void);
public:
/******************复制构造函数*******************/
MBString(MBString&src);
public:
/************************************************************************/
/**********************************隐式转换******************************/
/************************************************************************/
explicit MBString(int i);//explicit 需要显示调用该构造函数进行转换,阻止隐式转换
explicit MBString(char c);
explicit MBString(char* p);
explicit MBString(short int i);
explicit MBString(_struct_& s);
MBString(long long l);
public:
/************************************************************************/
/********************以下4个运算符重载时必须作为成员函数*****************/
/************************************************************************/
/************* =号重载格式为 T operator = (T1),其中T和T1可以为任意类型,也可为引用*************/
MBString& operator = (const MBString& srcObj);
/************************ []号重载,格式为 T operator [] (T1)***************************/
//const char& operator [] (int i);//返回值可以作为左值
//char& operator [] (int i);//返回值可以作为左值
char operator [] (int i);//返回值不能作为左值
/************************ ()号重载,格式为 T operator () (T1, T2,...)唯一一个参数可以为0-多个的运算符***************************/
//char* operator () (char c);
//char* operator () (int i, char c);
const char* operator () ();
/**************** ->重载, 格式为 T operator -> () 参数必须为空,返回值必须含有->对象***************/
//_struct_* operator -> ()//这个也是正确的
//{
// return m_pStruct;
//};
_struct_& operator -> ();
//{
// return *m_pStruct;
//};
/************************************************************************/
/***************以下运算符即可重载为成员函数,又可为友元函数**************/
/************************************************************************/
public:
/************************************************************************/
/***************************双目算术运算符重载***************************/
/************************************************************************/
/*************** +号重载,格式为 T operator + (T1)****************/
MBString operator + (const MBString& ms2);
/*************** -号重载,格式为 T operator - (T1)****************/
MBString operator - (const MBString& ms2);
/*************** *号重载,格式为 T operator * (T1)****************/
MBString operator * (const MBString& ms2);
/*************** /号重载,格式为 T operator / (T1)****************/
MBString operator / (const MBString& ms2);
/*************** %号重载,格式为 T operator % (T1)****************/
MBString operator % (const MBString& ms2);
public:
/************************************************************************/
/****************************关系运算符重载******************************/
/************************************************************************/
/***************** ==号重载, 格式为 T operator == (T1)*****************/
bool operator == (const MBString& ms2);
/***************** !=号重载, 格式为 T operator != (T1)*****************/
bool operator != (const MBString& ms2);
/***************** <号重载, 格式为 T operator < (T1)*****************/
bool operator < (const MBString& ms2);
/***************** <=号重载, 格式为 T operator <= (T1)*****************/
bool operator <= (const MBString& ms2);
/***************** >号重载, 格式为 T operator > (T1)*****************/
bool operator > (const MBString& ms2);
/***************** >=号重载, 格式为 T operator >= (T1)*****************/
bool operator >= (const MBString& ms2);
public:
/************************************************************************/
/******************************逻辑运算符重载****************************/
/************************************************************************/
/************* ||号重载,一般不重载,格式为 T operator || (T1)************/
bool operator || (const MBString& ms2);
/***************** &&号重载, 一般不重载,格式为 T operator && (T1)*****************/
bool operator && (const MBString& ms2);
/***************** !号重载,格式为 T operator ! ()*****************/
bool operator ! ();
public:
/************************************************************************/
/****************************单目运算符重载******************************/
/************************************************************************/
/***************** +(正)号重载, 格式为 T operator + ()*****************/
MBString& operator + ();
/***************** -(负)号重载, 格式为 T operator - ()*****************/
MBString& operator - ();
/***************** &号重载, 格式为 T operator & ()*****************/
MBString* operator & ();
/***************** *号重载, 格式为 T operator * ()*****************/
MBString& operator * ();
public:
/************************************************************************/
/***************************自增自减运算符重载***************************/
/************************************************************************/
/***************** 前置++号重载, 格式为 T operator ++ ()*****************/
MBString& operator ++ ();
/***************** 后置++号重载, 格式为 T operator ++ (int)*****************/
MBString operator ++ (int);
/***************** 前置--号重载, 格式为 T operator ++ ()*****************/
MBString& operator --();
/***************** 后置--号重载, 格式为 T operator ++ (int)*****************/
MBString operator -- (int);
public:
/************************************************************************/
/******************************位运算符**********************************/
/************************************************************************/
/***************** |号重载, 格式为 T operator | (T1)*****************/
MBString operator | (const MBString& ms2);
/***************** &号重载, 格式为 T operator & (T1)*****************/
MBString operator & (const MBString& ms2);
/***************** ^号重载, 格式为 T operator ^ (T1)*****************/
MBString operator ^ (const MBString& ms2);
/***************** <<号重载, 格式为 T operator << (T1)*****************/
MBString operator << (int i);
//friend ostream& operator >> (ostream&, MBString&);
/***************** <<号重载, 格式为 T operator << (T1)*****************/
MBString operator >> (int i);
//friend istream& operator >> (istream&, MBString&);
/***************** ~号重载, 格式为 T operator ~ ()*****************/
MBString operator ~ ();
public:
/************************************************************************/
/******************************赋值运算符********************************/
/************************************************************************/
/***************** +=号重载, 格式为 T operator += (T1)*****************/
MBString& operator += (const MBString& ms2);
/***************** -=号重载, 格式为 T operator -= (T1)*****************/
MBString& operator -= (const MBString& ms2);
/***************** *=号重载, 格式为 T operator *= (T1)*****************/
MBString& operator *= (const MBString& ms2);
/***************** /=号重载, 格式为 T operator /= (T1)*****************/
MBString& operator /= (const MBString& ms2);
/***************** %=号重载, 格式为 T operator %= (T1)*****************/
MBString& operator %= (const MBString& ms2);
/***************** &=号重载, 格式为 T operator &= (T1)*****************/
MBString& operator &= (const MBString& ms2);
/***************** |=号重载, 格式为 T operator |= (T1)*****************/
MBString& operator |= (const MBString& ms2);
/***************** ^=号重载, 格式为 T operator ^= (T1)*****************/
MBString& operator ^= (const MBString& ms2);
/***************** <<=号重载, 格式为 T operator <<= (T1)*****************/
MBString& operator <<= (int i);
/***************** >>=号重载, 格式为 T operator >>= (T1)*****************/
MBString& operator >>= (int i);
public:
/************************************************************************/
/***************************new,new[]和delete,delete[]的重载****************************/
/************************************************************************/
/**************重载格式 void* operator new(size_t size, T1,T2.....)***************/
void *operator new(size_t size);
void *operator new(size_t size, int i);
void *operator new[](size_t size);
void operator delete(void*p);
void operator delete(void*p, int i, int j);
void operator delete [](void* p);
public:
/************************************************************************/
/********类型转换函数格式为operator <const>type () <const>只能为成员函数********/
/************************************************************************/
/********** 强制转换为char* *********/
operator char* () const;//or operator char* const() or operator char* ()
/*********** 强制转换为int ***********/
operator int ();
/*********** 强制转换为char ************/
operator const char () const;
/*********** 强制转换为短整形 ************/
operator short int () const;
/*********** 强制转换为长整形 ************/
operator long long () const;
/*********** 强制转换为_struct_ *************/
operator _struct_ () const;
/*********** 强制转换为void *************/
operator void* () const;
public:
/************************************************************************/
/*****************************其他运算符重载*****************************/
/************************************************************************/
/************* ,运算符重载,一般不重载该运算符, 格式为 T operator , (T1)***************/
MBString& operator , (int i/*const MBString& ms*/);
/************成员指针运算符重载格式为 T operator ->* (T1)************/
MBString& operator ->* (char c);
const char* operator ->* (char c) const;
int fun() const
{
return 0;
};
//typedef int (MBString::*pmf)() const;
private:
_struct_* m_pStruct;
long long m_lLong;
int m_nInt;
short int m_nShort;
char* m_pChar;
char m_cChar;
void* m_vVoid;
};
#include "MBString.h"
/************************************************************************/
/**************************构造函数和析构函数****************************/
/************************************************************************/
MBString::MBString(void)
{
m_pStruct = (_struct_*)malloc(sizeof(_struct_));
m_pStruct->i = 1;
m_pStruct->son = (_structson_*)malloc(sizeof(_structson_));
m_pStruct->son->j = 2;
m_lLong = 0x0100000000000000;
m_nInt = 0x01000000;
m_nShort = 0x0100;
m_cChar = 0x01;
m_pChar = (char*) malloc(10);
memset(m_pChar, 0, 10);
m_vVoid = NULL;
}
MBString::~MBString(void)
{
if (NULL != m_pChar)
{
free(m_pChar);
m_pChar = NULL;
}
if (NULL != m_vVoid)
{
free(m_vVoid);
m_vVoid = NULL;
}
if (NULL != m_pStruct)
{
if (NULL != m_pStruct->son)
{
free(m_pStruct->son);
m_pStruct->son = NULL;
}
free(m_pStruct);
m_pStruct = NULL;
}
}
/************************************************************************/
/*******************************复制构造函数*****************************/
/************************************************************************/
MBString::MBString(MBString& src)
{
m_lLong = src.m_lLong;
m_nInt = src.m_nInt;
m_nShort = src.m_nShort;
m_cChar = src.m_cChar;
if (NULL == src.m_pChar)
{
m_pChar = NULL;
}
else
{
m_pChar = (char*)malloc(strlen(src.m_pChar)+1);
memcpy(m_pChar, src.m_pChar, strlen(src.m_pChar));
m_pChar[strlen(src.m_pChar)] = 0;
}
if (NULL == src.m_vVoid)
{
m_vVoid = NULL;
}
else
{
m_vVoid = malloc(strlen((char*)src.m_vVoid)+1);
memcpy(m_vVoid, src.m_vVoid,strlen((char*)src.m_vVoid));
((char*)m_vVoid)[strlen((char*)src.m_vVoid)] = 0;
}
if (NULL == src.m_pStruct)
{
m_pStruct = NULL;
}
else
{
m_pStruct = (_struct_*)malloc(sizeof(_struct_));
m_pStruct->i = src.m_pStruct->i;
if (NULL == src.m_pStruct->son)
{
m_pStruct->son = NULL;
}
else
{
m_pStruct->son = (_structson_*)malloc(sizeof(_structson_));
m_pStruct->son->j = src.m_pStruct->son->j;
}
}
}
/************************************************************************/
/********************************隐式转换********************************/
/************************************************************************/
MBString::MBString(int i)
{
m_pStruct = NULL;
m_lLong = 0;
m_nInt = i;
m_nShort = 0;
m_pChar = NULL;
m_cChar = 0;
m_vVoid = NULL;
}
MBString::MBString(char c)
{
m_pStruct = NULL;
m_lLong = 0;
m_nInt = 0;
m_nShort = 0;
m_pChar = NULL;
m_cChar = c;
m_vVoid = NULL;
}
MBString::MBString(char* p)
{
m_pStruct = NULL;
m_lLong = 0;
m_nInt = 0;
m_nShort = 0;
m_cChar = 0;
m_vVoid = NULL;
m_pChar = (char*)malloc(strlen(p)+1);
memcpy(m_pChar, p, strlen(p));
m_pChar[strlen(p)] = 0;
}
MBString::MBString(short int i)
{
m_pStruct = NULL;
m_lLong = 0;
m_nInt = 0;
m_nShort = i;
m_pChar = NULL;
m_cChar = 0;
m_vVoid = NULL;
}
MBString::MBString(_struct_& s)
{
m_lLong = 0;
m_nInt = 0;
m_nShort = 0;
m_pChar = NULL;
m_cChar = 0;
m_vVoid = NULL;
m_pStruct = (_struct_*)malloc(sizeof(_struct_));
m_pStruct->i = s.i;
if (NULL == s.son)
{
m_pStruct->son = NULL;
}
else
{
m_pStruct->son = (_structson_*)malloc(sizeof(_structson_));
m_pStruct->son->j = s.son->j;
}
}
MBString::MBString(long long l)
{
m_pStruct = NULL;
m_lLong = l;
m_nInt = 0;
m_nShort = 0;
m_pChar = NULL;
m_cChar = 0;
m_vVoid = NULL;
}
/************************************************************************/
/********************以下4个运算符重载时必须作为成员函数*****************/
/************************************************************************/
MBString& MBString::operator = (const MBString& srcObj)
{
if (this != &srcObj)
{
m_lLong = srcObj.m_lLong;
m_nInt = srcObj.m_nInt;
m_nShort = srcObj.m_nShort;
m_cChar = srcObj.m_cChar;
if (NULL != m_pChar)
{
free(m_pChar);
m_pChar = NULL;
}
if (NULL == srcObj.m_pChar)
{
m_pChar = NULL;
}
else
{
m_pChar = (char*)malloc(strlen(srcObj.m_pChar)+1);
memcpy(m_pChar, srcObj.m_pChar, strlen(srcObj.m_pChar));
m_pChar[strlen(srcObj.m_pChar)] = 0;
}
if (NULL != m_vVoid)
{
free(m_vVoid);
m_vVoid = NULL;
}
if (NULL == srcObj.m_vVoid)
{
m_vVoid = NULL;
}
else
{
m_vVoid = malloc(strlen((char*)srcObj.m_vVoid)+1);
memcpy(m_vVoid, srcObj.m_vVoid,strlen((char*)srcObj.m_vVoid));
((char*)m_vVoid)[strlen((char*)srcObj.m_vVoid)] = 0;
}
if (NULL != m_pStruct)
{
if (NULL != m_pStruct->son)
{
free(m_pStruct->son);
}
free(m_pStruct);
m_pStruct = NULL;
}
if (NULL == srcObj.m_pStruct)
{
m_pStruct = NULL;
}
else
{
m_pStruct = (_struct_*)malloc(sizeof(_struct_));
m_pStruct->i = srcObj.m_pStruct->i;
if (NULL == srcObj.m_pStruct->son)
{
m_pStruct->son = NULL;
}
else
{
m_pStruct->son = (_structson_*)malloc(sizeof(_structson_));
m_pStruct->son->j = srcObj.m_pStruct->son->j;
}
}
}
return *this;
}
char MBString::operator [] (int i)
{
if (NULL == m_pChar)
{
return '\0';
}
if (i > (strlen(m_pChar)-1))
{
return '\0';
}
else
{
return m_pChar[i];
}
}
const char* MBString::operator () ()
{
return m_pChar;
}
_struct_& MBString::operator -> ()
{
return *m_pStruct;
}
/************************************************************************/
/***************以下运算符即可重载为成员函数,又可为友元函数**************/
/************************************************************************/
/************************************************************************/
/***************************双目算术运算符重载***************************/
/************************************************************************/
/*************** +号重载,格式为 T operator + (T1)****************/
MBString MBString::operator + (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt+ms2.m_nInt;
tmp.m_lLong = m_lLong+ms2.m_lLong;
tmp.m_nShort = m_nShort+ms2.m_nShort;
return tmp;
}
/*************** -号重载,格式为 T operator - (T1)****************/
MBString MBString::operator - (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt-ms2.m_nInt;
tmp.m_lLong = m_lLong-ms2.m_lLong;
tmp.m_nShort = m_nShort-ms2.m_nShort;
return tmp;
}
/*************** *号重载,格式为 T operator * (T1)****************/
MBString MBString::operator * (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt*ms2.m_nInt;
tmp.m_lLong = m_lLong*ms2.m_lLong;
tmp.m_nShort = m_nShort*ms2.m_nShort;
return tmp;
}
/*************** /号重载,格式为 T operator / (T1)****************/
MBString MBString::operator / (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt/ms2.m_nInt;
tmp.m_lLong = m_lLong/ms2.m_lLong;
tmp.m_nShort = m_nShort/ms2.m_nShort;
return tmp;
}
/*************** %号重载,格式为 T operator % (T1)****************/
MBString MBString::operator % (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt%ms2.m_nInt;
tmp.m_lLong = m_lLong%ms2.m_lLong;
tmp.m_nShort = m_nShort%ms2.m_nShort;
return tmp;
}
/************************************************************************/
/****************************关系运算符重载******************************/
/************************************************************************/
/***************** ==号重载, 格式为 T operator == (T1)*****************/
bool MBString::operator == (const MBString& ms2)
{
return m_nInt == ms2.m_nInt;
}
/***************** !=号重载, 格式为 T operator != (T1)*****************/
bool MBString::operator != (const MBString& ms2)
{
return m_nInt != ms2.m_nInt;
}
/***************** <号重载, 格式为 T operator < (T1)*****************/
bool MBString::operator < (const MBString& ms2)
{
return m_nInt < ms2.m_nInt;
}
/***************** <=号重载, 格式为 T operator <= (T1)*****************/
bool MBString::operator <= (const MBString& ms2)
{
return m_nInt <= ms2.m_nInt;
}
/***************** >号重载, 格式为 T operator > (T1)*****************/
bool MBString::operator > (const MBString& ms2)
{
return m_nInt > ms2.m_nInt;
}
/***************** >=号重载, 格式为 T operator >= (T1)*****************/
bool MBString::operator >= (const MBString& ms2)
{
return m_nInt >= ms2.m_nInt;
}
/************************************************************************/
/******************************逻辑运算符重载****************************/
/************************************************************************/
/***************** ||号重载, 格式为 T operator || (T1)*****************/
bool MBString::operator || (const MBString& ms2)
{
return (NULL == m_pChar || NULL == ms2.m_pChar);
}
/***************** &&号重载, 格式为 T operator && (T1)*****************/
bool MBString::operator && (const MBString& ms2)
{
return (NULL == m_pChar && NULL == ms2.m_pChar);
}
/***************** !号重载, 格式为 T operator ! ()*****************/
bool MBString::operator ! ()
{
return !m_pChar;
}
/************************************************************************/
/****************************单目运算符重载******************************/
/************************************************************************/
/***************** +(正)号重载, 格式为 T operator + ()*****************/
MBString& MBString::operator + ()
{
return *this;
}
/***************** -(负)号重载, 格式为 T operator - ()*****************/
MBString& MBString::operator - ()
{
m_nInt = - m_nInt;
return *this;
}
/***************** &号重载, 格式为 T operator & ()*****************/
MBString* MBString::operator & ()
{
return this;
}
/***************** *号重载, 格式为 T operator * ()*****************/
MBString& MBString::operator * ()
{
return *this;
}
/************************************************************************/
/***************************自增自减运算符重载***************************/
/************************************************************************/
/***************** 前置++号重载, 格式为 T operator ++ ()*****************/
MBString& MBString::operator ++ ()
{
m_lLong++;
m_nInt++;
m_nShort++;
m_pChar++;
return *this;
}
/***************** 后置++号重载, 格式为 T operator ++ ()*****************/
MBString MBString::operator ++ (int)
{
MBString tmp(*this);
m_lLong++;
m_nInt++;
m_nShort++;
m_pChar++;
return tmp;
}
/***************** 前置--号重载, 格式为 T operator ++ ()*****************/
MBString& MBString::operator --()
{
m_lLong--;
m_nInt--;
m_nShort--;
m_pChar--;
return *this;
}
/***************** 后置--号重载, 格式为 T operator ++ ()*****************/
MBString MBString::operator -- (int)
{
MBString tmp(*this);
m_lLong--;
m_nInt--;
m_nShort--;
m_pChar--;
return tmp;
}
/************************************************************************/
/******************************位运算符**********************************/
/************************************************************************/
/***************** |号重载, 格式为 T operator | (T1)*****************/
MBString MBString::operator | (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt|ms2.m_nInt;
tmp.m_lLong = m_lLong|ms2.m_lLong;
tmp.m_nShort = m_nShort|ms2.m_nShort;
return tmp;
}
/***************** &号重载, 格式为 T operator & (T1)*****************/
MBString MBString::operator & (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt&ms2.m_nInt;
tmp.m_lLong = m_lLong&ms2.m_lLong;
tmp.m_nShort = m_nShort&ms2.m_nShort;
return tmp;
}
/***************** ^号重载, 格式为 T operator ^ (T1)*****************/
MBString MBString::operator ^ (const MBString& ms2)
{
MBString tmp;
tmp.m_nInt = m_nInt^ms2.m_nInt;
tmp.m_lLong = m_lLong^ms2.m_lLong;
tmp.m_nShort = m_nShort^ms2.m_nShort;
return tmp;
}
/***************** <<号重载, 格式为 T operator << (T1)*****************/
MBString MBString::operator << (int i)
{
MBString tmp(*this);
tmp.m_nInt = m_nInt<<i;
tmp.m_lLong = m_lLong<<i;
tmp.m_nShort = m_nShort<<i;
return tmp;
}
//friend ostream& operator >> (ostream&, MBString&);
/***************** <<号重载, 格式为 T operator << (T1)*****************/
MBString MBString::operator >> (int i)
{
MBString tmp(*this);
tmp.m_nInt = m_nInt>>i;
tmp.m_lLong = m_lLong>>i;
tmp.m_nShort = m_nShort>>i;
return tmp;
}
//friend istream& operator >> (istream&, MBString&);
/***************** ~号重载, 格式为 T operator ~ ()*****************/
MBString MBString::operator ~ ()
{
MBString tmp(*this);
tmp.m_nInt = ~m_nInt;
tmp.m_lLong = ~m_lLong;
tmp.m_nShort = ~m_nShort;
return tmp;
}
/************************************************************************/
/******************************赋值运算符********************************/
/************************************************************************/
/***************** +=号重载, 格式为 T operator += (T1)*****************/
MBString& MBString::operator += (const MBString& ms2)
{
m_lLong+=ms2.m_lLong;
m_nInt+=ms2.m_nInt;
m_nShort+=ms2.m_nShort;
return *this;
}
/***************** -=号重载, 格式为 T operator -= (T1)*****************/
MBString& MBString::operator -= (const MBString& ms2)
{
m_lLong-=ms2.m_lLong;
m_nInt-=ms2.m_nInt;
m_nShort-=ms2.m_nShort;
return *this;
}
/***************** *=号重载, 格式为 T operator *= (T1)*****************/
MBString& MBString::operator *= (const MBString& ms2)
{
m_lLong*=ms2.m_lLong;
m_nInt*=ms2.m_nInt;
m_nShort*=ms2.m_nShort;
return *this;
}
/***************** /=号重载, 格式为 T operator /= (T1)*****************/
MBString& MBString::operator /= (const MBString& ms2)
{
m_lLong/=ms2.m_lLong;
m_nInt/=ms2.m_nInt;
m_nShort/=ms2.m_nShort;
return *this;
}
/***************** %=号重载, 格式为 T operator %= (T1)*****************/
MBString& MBString::operator %= (const MBString& ms2)
{
m_lLong%=ms2.m_lLong;
m_nInt%=ms2.m_nInt;
m_nShort%=ms2.m_nShort;
return *this;
}
/***************** &=号重载, 格式为 T operator &= (T1)*****************/
MBString& MBString::operator &= (const MBString& ms2)
{
m_lLong&=ms2.m_lLong;
m_nInt&=ms2.m_nInt;
m_nShort&=ms2.m_nShort;
return *this;
}
/***************** |=号重载, 格式为 T operator |= (T1)*****************/
MBString& MBString::operator |= (const MBString& ms2)
{
m_lLong|=ms2.m_lLong;
m_nInt|=ms2.m_nInt;
m_nShort|=ms2.m_nShort;
return *this;
}
/***************** ^=号重载, 格式为 T operator ^= (T1)*****************/
MBString& MBString::operator ^= (const MBString& ms2)
{
m_lLong^=ms2.m_lLong;
m_nInt^=ms2.m_nInt;
m_nShort^=ms2.m_nShort;
return *this;
}
/***************** <<=号重载, 格式为 T operator <<= (T1)*****************/
MBString& MBString::operator <<= (int i)
{
m_lLong<<=i;
m_nInt<<=i;
m_nShort<<=i;
return *this;
}
/***************** >>=号重载, 格式为 T operator >>= (T1)*****************/
MBString& MBString::operator >>= (int i)
{
m_lLong>>=i;
m_nInt>>=i;
m_nShort>>=i;
return *this;
}
/************************************************************************/
/***************************new,new[]和delete,delete[]的重载****************************/
/************************************************************************/
void* MBString::operator new(size_t size)
{
//return ::operator new(size);
return malloc(size);
};
void* MBString::operator new(size_t size, int i)
{
return malloc(size);
}
void* MBString::operator new[](size_t size)
{
return malloc(size);
}
void MBString::operator delete(void*p)
{
free(p);
};
void MBString::operator delete(void*p, int i, int j)
{
free(p);
}
void MBString::operator delete [](void* p)
{
free(p);
}
/************************************************************************/
/**************类型转换函数格式为operator type () <const>*************/
/************************************************************************/
/********** 强制转换为char* *********/
MBString::operator char* () const
{
return m_pChar;
}//or operator char* const() or operator char* ()
/*********** 强制转换为int ***********/
MBString::operator int ()
{
return m_nInt;
}
/*********** 强制转换为char ************/
MBString::operator const char () const
{
return m_cChar;
}
/*********** 强制转换为短整形 ************/
MBString::operator short int () const
{
return m_nShort;
}
/*********** 强制转换为长整形 ************/
MBString::operator long long () const
{
return m_lLong;
}
/*********** 强制转换为_struct_ *************/
MBString::operator _struct_ () const
{
return *m_pStruct;
}
/*********** 强制转换为void *************/
MBString::operator void* () const
{
return m_vVoid;
}
/************************************************************************/
/*****************************其他运算符重载*****************************/
/************************************************************************/
MBString& MBString::operator , (int i)
{
return *this;
}
MBString& MBString::operator ->* (char c)
{
return *this;
}
const char* MBString::operator ->* (char c) const
{
return m_pChar;
}
重载为友元函数为:
/********************************************************************
created: 2012/03/28
created: 28:3:2012 15:23
filename: f:\work\testOperatorOverload\testOperatorOverload\FBString.h
file path: f:\work\testOperatorOverload\testOperatorOverload
file base: FBString
file ext: h
author: dyj
purpose: 学习重载运算符,本类实现的都是重载运算符作为友元函数
*********************************************************************/
#pragma once
#include <iostream>
using namespace std;
struct _structFB_//测试FBString类重载->使用
{
int i;
};
class FBString
{
public:
FBString(void);
~FBString(void);
public:
/*复制构造函数*/
FBString(const FBString& src);
public:
/************************************************************************/
/********************以下2个运算符重载时必须作为友元函数*****************/
/************************************************************************/
/*************流输入运算符格式为 friend istream& operator >> (istream& , T& src) ****************/
friend istream& operator >> (istream& inputs, FBString& src);
//friend istream& operator >> (istream& inputs, _structFB_ i);//这个也行不过cin>>_structFB_类型的
/*************流输出运算符格式为 friend ostream& operator << (ostream& , <const>T& src) ****************/
friend ostream& operator << (ostream& outputs, const FBString& src);
public:
/************************************************************************/
/****************双目算术运算符重载T1和T2必须有一个是类对象****************/
/************************************************************************/
/*************** +号重载,格式为 friend T operator + (T1, T2)****************/
friend FBString operator + (const FBString& fs1,const FBString& fs2);
/*************** -号重载,格式为 friend T operator - (T1, T2)****************/
friend FBString operator - (const FBString& fs1,const FBString& fs2);
/*************** *号重载,格式为 friend T operator * (T1, T2)****************/
friend FBString operator * (const FBString& fs1,const FBString& fs2);
/*************** /号重载,格式为 friend T operator / (T1, T2)****************/
friend FBString operator / (const FBString& fs1,const FBString& fs2);
/*************** %号重载,格式为 friend T operator % (T1, T2)****************/
friend FBString operator % (const FBString& fs1,const FBString& fs2);
public:
/************************************************************************/
/****************关系运算符重载T1和T2必须有一个是类对象******************/
/************************************************************************/
/***************** ==号重载, 格式为 friend T operator == (T1,T2)*****************/
friend bool operator == (const FBString& fs1, const FBString& fs2);
/***************** !=号重载, 格式为 friend T operator != (T1,T2)*****************/
friend bool operator != (const FBString& fs1, const FBString& fs2);
/***************** <号重载, 格式为 friend T operator < (T1,T2)*****************/
friend bool operator < (const FBString& fs1, const FBString& fs2);
/***************** <=号重载, 格式为 friend T operator <= (T1,T2)*****************/
friend bool operator <= (const FBString& fs1, const FBString& fs2);
/***************** >号重载, 格式为 friend T operator > (T1,T2)*****************/
friend bool operator > (const FBString& fs1, const FBString& fs2);
/***************** >=号重载, 格式为 friend T operator >= (T1,T2)*****************/
friend bool operator >= (const FBString& fs1, const FBString& fs2);
public:
/************************************************************************/
/*****************逻辑运算符重载T1和T2必须有一个是类对象*****************/
/************************************************************************/
/************* ||号重载,一般不重载,格式为 friend T operator || (T1,T2)************/
friend bool operator || (const FBString& fs1, const FBString& fs2);
/***************** &&号重载, 一般不重载,格式为 friend T operator && (T1,T2)*****************/
friend bool operator && (const FBString& fs1, const FBString& fs2);
/***************** !号重载,格式为 friend T operator ! (T1)*****************/
friend bool operator ! (const FBString& fs);
public:
/************************************************************************/
/****************************单目运算符重载******************************/
/************************************************************************/
/***************** +(正)号重载, 格式为 friend T operator + (T1)*****************/
friend FBString operator + (FBString& fs);
/***************** -(负)号重载, 格式为 friend T operator - (T1)*****************/
friend FBString operator - (FBString& fs);
/***************** &号重载, 格式为 friend T operator & (T1)*****************/
friend FBString* operator & (FBString& fs);
/***************** *号重载, 格式为 friend T operator * (T1)*****************/
friend FBString operator * (FBString& fs);
public:
/************************************************************************/
/***************************自增自减运算符重载***************************/
/************************************************************************/
/***************** 前置++号重载, 格式为 friend T operator ++ (T1)*****************/
friend FBString& operator ++ (FBString& fs);
/***************** 后置++号重载, 格式为 friend T operator ++ (T1,int)*****************/
friend FBString operator ++ (FBString& fs,int);
/***************** 前置--号重载, 格式为 friend T operator ++ (T1)*****************/
friend FBString& operator --(FBString& fs);
/***************** 后置--号重载, 格式为 friend T operator ++ (T1,int)*****************/
friend FBString operator -- (FBString& fs,int);
public:
/************************************************************************/
/**********************位运算符T1,T2必须有个类对象***********************/
/************************************************************************/
/***************** |号重载, 格式为 friend T operator | (T1,T2)*****************/
friend FBString operator | (const FBString& fs1, const FBString& fs2);
/***************** &号重载, 格式为 friend T operator & (T1,T2)*****************/
friend FBString operator & (const FBString& fs1, const FBString& fs2);
/***************** ^号重载, 格式为 friend T operator ^ (T1,T2)*****************/
friend FBString operator ^ (const FBString& fs1, const FBString& fs2);
/***************** <<号重载, 格式为 friend T operator << (T1,T2)*****************/
friend FBString operator << (const FBString& fs1, int i);
//friend FBString operator << (int i,const FBString& fs1);//这种也没问题,只要你会玩
/***************** <<号重载, 格式为 friend T operator << (T1,T2)*****************/
friend FBString operator >> (const FBString& fs1, int i);
/***************** ~号重载, 格式为 friend T operator ~ (T1)*****************/
friend FBString operator ~ (const FBString& fs1);
public:
/************************************************************************/
/*********************赋值运算符T1,T2必须有个类对象**********************/
/************************************************************************/
/***************** +=号重载, 格式为 friend T operator += (T1,T2)*****************/
friend FBString& operator += (FBString& fs1, const FBString& fs2);
/***************** -=号重载, 格式为 friend T operator -= (T1,T2)*****************/
friend FBString& operator -= (FBString& fs1, const FBString& fs2);
/***************** *=号重载, 格式为 friend T operator *= (T1,T2)*****************/
friend FBString& operator *= (FBString& fs1, const FBString& fs2);
/***************** /=号重载, 格式为 friend T operator /= (T1,T2)*****************/
friend FBString& operator /= (FBString& fs1, const FBString& fs2);
/***************** %=号重载, 格式为 friend T operator %= (T1,T2)*****************/
friend FBString& operator %= (FBString& fs1, const FBString& fs2);
/***************** &=号重载, 格式为 friend T operator &= (T1,T2)*****************/
friend FBString& operator &= (FBString& fs1, const FBString& fs2);
/***************** |=号重载, 格式为 friend T operator |= (T1,T2)*****************/
friend FBString& operator |= (FBString& fs1, const FBString& fs2);
/***************** ^=号重载, 格式为 friend T operator ^= (T1,T2)*****************/
friend FBString& operator ^= (FBString& fs1, const FBString& fs2);
/***************** <<=号重载, 格式为 friend T operator <<= (T1,T2)*****************/
friend FBString& operator <<= (FBString& fs1, int i);
/***************** >>=号重载, 格式为 friend T operator >>= (T1,T2)*****************/
friend FBString& operator >>= (FBString& fs1, int i);
public:
/************************************************************************/
/*****************************其他运算符重载*****************************/
/************************************************************************/
/************* ,运算符重载,一般不重载该运算符, 格式为 friend T operator , (T1,T2)***************/
friend FBString& operator , (FBString& fs1, FBString& fs2);
/************成员指针运算符重载格式为 friend T operator ->* (T1,T2)************/
friend const _structFB_* operator ->* (const FBString& fs1, char c);
public:
friend void* operator new (size_t size) throw(bad_alloc)//相当于全局的重载
{
return NULL;
}
void* operator new (size_t size)//和上面的friend同时拥有该类的new会屏蔽上面的friend
{
return malloc(size);
}
//friend void* operator new (size_t t, void* location)
//{
// return location;
//}
//void* operator new[] (size_t t)
//{
// return NULL;
//}
//void* operator new[] (size_t t, void* location)
//{
// return location;
//}
private:
int m_nInt;
short int m_stShort;
_structFB_ m_fbs;
};
#include "FBString.h"
FBString::FBString(void)
{
m_nInt = 8;
m_stShort = 2;
m_fbs.i = 5;
}
FBString::~FBString(void)
{
int j = 0;
}
FBString::FBString(const FBString& src)
{
m_nInt = src.m_nInt;
m_stShort = src.m_stShort;
}
/************************************************************************/
/********************以下2个运算符重载时必须作为友元函数*****************/
/************************************************************************/
/*************流输入运算符格式为 friend istream& operator >> (istream& , T& src) ****************/
istream& operator >> (istream& inputs, FBString& src)
{
cout << "请输入2个整形数据:"<<endl;
inputs >> src.m_nInt >> src.m_stShort;
return inputs;
}
/*************流输出运算符格式为 friend ostream& operator << (ostream& , <const>T& src) ****************/
ostream& operator << (ostream& outputs, const FBString& src)
{
outputs << "src.m_nInt = " <<src.m_nInt << endl;
outputs << "src.m_stShort = " << src.m_stShort << endl;
return outputs;
}
/************************************************************************/
/****************双目算术运算符重载T1和T2必须有一个是类对象****************/
/************************************************************************/
/*************** +号重载,格式为 friend T operator + (T1, T2)****************/
FBString operator + (const FBString& fs1,const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt + fs2.m_nInt;
c.m_stShort = fs1.m_stShort + fs2.m_stShort;
return c;
}
/*************** -号重载,格式为 friend T operator - (T1, T2)****************/
FBString operator - (const FBString& fs1,const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt - fs2.m_nInt;
c.m_stShort = fs1.m_stShort - fs2.m_stShort;
return c;
}
/*************** *号重载,格式为 friend T operator * (T1, T2)****************/
FBString operator * (const FBString& fs1,const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt * fs2.m_nInt;
c.m_stShort = fs1.m_stShort * fs2.m_stShort;
return c;
}
/*************** /号重载,格式为 friend T operator / (T1, T2)****************/
FBString operator / (const FBString& fs1,const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt / fs2.m_nInt;
c.m_stShort = fs1.m_stShort / fs2.m_stShort;
return c;
}
/*************** %号重载,格式为 friend T operator % (T1, T2)****************/
FBString operator % (const FBString& fs1,const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt % fs2.m_nInt;
c.m_stShort = fs1.m_stShort % fs2.m_stShort;
return c;
}
/************************************************************************/
/****************关系运算符重载T1和T2必须有一个是类对象******************/
/************************************************************************/
/***************** ==号重载, 格式为 friend T operator == (T1,T2)*****************/
bool operator == (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt == fs2.m_nInt) && (fs1.m_stShort == fs2.m_stShort));
}
/***************** !=号重载, 格式为 friend T operator != (T1,T2)*****************/
bool operator != (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt != fs2.m_nInt) && (fs1.m_stShort != fs2.m_stShort));
}
/***************** <号重载, 格式为 friend T operator < (T1,T2)*****************/
bool operator < (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt < fs2.m_nInt) && (fs1.m_stShort < fs2.m_stShort));
}
/***************** <=号重载, 格式为 friend T operator <= (T1,T2)*****************/
bool operator <= (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt <= fs2.m_nInt) && (fs1.m_stShort <= fs2.m_stShort));
}
/***************** >号重载, 格式为 friend T operator > (T1,T2)*****************/
bool operator > (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt > fs2.m_nInt) && (fs1.m_stShort > fs2.m_stShort));
}
/***************** >=号重载, 格式为 friend T operator >= (T1,T2)*****************/
bool operator >= (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt>= fs2.m_nInt) && (fs1.m_stShort >= fs2.m_stShort));
}
/************************************************************************/
/*****************逻辑运算符重载T1和T2必须有一个是类对象*****************/
/************************************************************************/
/************* ||号重载,一般不重载,格式为 friend T operator || (T1,T2)************/
bool operator || (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt|| fs2.m_nInt) && (fs1.m_stShort || fs2.m_stShort));
}
/***************** &&号重载, 一般不重载,格式为 friend T operator && (T1,T2)*****************/
bool operator && (const FBString& fs1, const FBString& fs2)
{
return ((fs1.m_nInt && fs2.m_nInt) && (fs1.m_stShort && fs2.m_stShort));
}
/***************** !号重载,格式为 friend T operator ! (T1)*****************/
bool operator ! (const FBString& fs)
{
return ((!fs.m_nInt) && (!fs.m_stShort));
}
/************************************************************************/
/****************************单目运算符重载******************************/
/************************************************************************/
/***************** +(正)号重载, 格式为 friend T operator + (T1)*****************/
FBString operator + (FBString& fs)
{
return fs;
}
/***************** -(负)号重载, 格式为 friend T operator - (T1)*****************/
FBString operator - (FBString& fs)
{
FBString c;
c.m_nInt = (-fs.m_nInt);
c.m_stShort = (-fs.m_stShort);
return c;
}
/***************** &号重载, 格式为 friend T operator & (T1)*****************/
FBString* operator & (FBString& fs)
{
return &fs;
}
/***************** *号重载, 格式为 friend T operator * (T1)*****************/
FBString operator * (FBString& fs)
{
return fs;
}
/************************************************************************/
/***************************自增自减运算符重载***************************/
/************************************************************************/
/***************** 前置++号重载, 格式为 friend T operator ++ (T1)*****************/
FBString& operator ++ (FBString& fs)
{
++fs.m_nInt;
++fs.m_stShort;
return fs;
}
/***************** 后置++号重载, 格式为 friend T operator ++ (T1,int)*****************/
FBString operator ++ (FBString& fs,int)
{
FBString c = fs;
++fs.m_nInt;
++fs.m_stShort;
return c;
}
/***************** 前置--号重载, 格式为 friend T operator ++ (T1)*****************/
FBString& operator --(FBString& fs)
{
--fs.m_nInt;
--fs.m_stShort;
return fs;
}
/***************** 后置--号重载, 格式为 friend T operator ++ (T1,int)*****************/
FBString operator -- (FBString& fs,int)
{
FBString c;
--fs.m_nInt;
--fs.m_stShort;
return c;
}
/************************************************************************/
/**********************位运算符T1,T2必须有个类对象***********************/
/************************************************************************/
/***************** |号重载, 格式为 friend T operator | (T1,T2)*****************/
FBString operator | (const FBString& fs1, const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt | fs2.m_nInt;
c.m_stShort = fs1.m_stShort | fs2.m_stShort;
return c;
}
/***************** &号重载, 格式为 friend T operator & (T1,T2)*****************/
FBString operator & (const FBString& fs1, const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt & fs2.m_nInt;
c.m_stShort = fs1.m_stShort & fs2.m_stShort;
return c;
}
/***************** ^号重载, 格式为 friend T operator ^ (T1,T2)*****************/
FBString operator ^ (const FBString& fs1, const FBString& fs2)
{
FBString c;
c.m_nInt = fs1.m_nInt ^ fs2.m_nInt;
c.m_stShort = fs1.m_stShort ^ fs2.m_stShort;
return c;
}
/***************** <<号重载, 格式为 friend T operator << (T1,T2)*****************/
FBString operator << (const FBString& fs1, int i)
{
FBString c;
c.m_nInt = fs1.m_nInt << i;
c.m_stShort = fs1.m_stShort << i;
return c;
}
//friend FBString operator << (int i,const FBString& fs1);//这种也没问题,只要你会玩
/***************** <<号重载, 格式为 friend T operator << (T1,T2)*****************/
FBString operator >> (const FBString& fs1, int i)
{
FBString c;
c.m_nInt = fs1.m_nInt >> i;
c.m_stShort = fs1.m_stShort >> i;
return c;
}
/***************** ~号重载, 格式为 friend T operator ~ (T1)*****************/
FBString operator ~ (const FBString& fs1)
{
FBString c;
c.m_nInt = ~fs1.m_nInt;
c.m_stShort = ~fs1.m_stShort;
return c;
}
/************************************************************************/
/*********************赋值运算符T1,T2必须有个类对象**********************/
/************************************************************************/
/***************** +=号重载, 格式为 friend T operator += (T1,T2)*****************/
FBString& operator += (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt+=fs2.m_nInt;
fs1.m_stShort+=fs2.m_stShort;
return fs1;
}
/***************** -=号重载, 格式为 friend T operator -= (T1,T2)*****************/
FBString& operator -= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt-=fs2.m_nInt;
fs1.m_stShort-=fs2.m_stShort;
return fs1;
}
/***************** *=号重载, 格式为 friend T operator *= (T1,T2)*****************/
FBString& operator *= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt*=fs2.m_nInt;
fs1.m_stShort*=fs2.m_stShort;
return fs1;
}
/***************** /=号重载, 格式为 friend T operator /= (T1,T2)*****************/
FBString& operator /= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt/=fs2.m_nInt;
fs1.m_stShort/=fs2.m_stShort;
return fs1;
}
/***************** %=号重载, 格式为 friend T operator %= (T1,T2)*****************/
FBString& operator %= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt%=fs2.m_nInt;
fs1.m_stShort%=fs2.m_stShort;
return fs1;
}
/***************** &=号重载, 格式为 friend T operator &= (T1,T2)*****************/
FBString& operator &= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt&=fs2.m_nInt;
fs1.m_stShort&=fs2.m_stShort;
return fs1;
}
/***************** |=号重载, 格式为 friend T operator |= (T1,T2)*****************/
FBString& operator |= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt|=fs2.m_nInt;
fs1.m_stShort|=fs2.m_stShort;
return fs1;
}
/***************** ^=号重载, 格式为 friend T operator ^= (T1,T2)*****************/
FBString& operator ^= (FBString& fs1, const FBString& fs2)
{
fs1.m_nInt^=fs2.m_nInt;
fs1.m_stShort^=fs2.m_stShort;
return fs1;
}
/***************** <<=号重载, 格式为 friend T operator <<= (T1,T2)*****************/
FBString& operator <<= (FBString& fs1, int i)
{
fs1.m_nInt<<=i;
fs1.m_stShort<<=i;
return fs1;
}
/***************** >>=号重载, 格式为 friend T operator >>= (T1,T2)*****************/
FBString& operator >>= (FBString& fs1, int i)
{
fs1.m_nInt>>=i;
fs1.m_stShort>>=i;
return fs1;
}
/************************************************************************/
/*****************************其他运算符重载*****************************/
/************************************************************************/
/************* ,运算符重载,一般不重载该运算符, 格式为 friend T operator , (T1,T2)***************/
FBString& operator , (FBString& fs1, FBString& fs2)
{
return fs2;
}
/************成员指针运算符重载格式为 friend T operator ->* (T1,T2)************/
const _structFB_* operator ->* (const FBString& fs1, char c)
{
return &(fs1.m_fbs);
}