C++ Primer 学习笔记_27_操作符重载与转换(2)--++/--运算符重载、!运算符重载、赋值运算符重载 、String类([]、 +、 += 运算符重载)、>>和<<运算符重载
1、前置++运算符重载
成员函数的方式重载,原型为:
函数类型 & operator++();
友元函数的方式重载,原型为:
friend 函数类型 & operator++(类类型 &);
2、后置++运算符重载
成员函数的方式重载,原型为:
函数类型 operator++(int);
友元函数的方式重载,原型为:
friend 函数类型 operator++(类类型 &, int);
3、前置--运算符重载
成员函数的方式重载,原型为:
函数类型 & operator--();
友元函数的方式重载,原型为:
friend 函数类型 & operator--(类类型 &);
4、后置--运算符重载
成员函数的方式重载,原型为:
函数类型 operator--(int);
友元函数的方式重载,原型为:
friend 函数类型 operator--(类类型 &, int);
#include <iostream>
using namespace std;
class Integer
{
public:
Integer(int n);
~Integer();
Integer &operator++();
//friend Integer& operator++(Integer& i);
Integer operator++(int n);
//friend Integer operator++(Integer& i, int n);
Integer &operator--();
//friend Integer& operator--(Integer& i);
Integer operator--(int n);
//friend Integer operator--(Integer& i, int n);
void Display() const;
private:
int n_;
};
Integer::Integer(int n) : n_(n) { }
Integer::~Integer() { }
Integer &Integer::operator ++()
{
++n_;
return *this;
}
//Integer& operator++(Integer& i)
//{
// ++i.n_;
// return i;
//}
Integer Integer::operator++(int n)
{
//n_++;
Integer tmp(n_);
n_++;
return tmp;
}
//Integer operator++(Integer& i, int n)
//{
// Integer tmp(i.n_);
// i.n_++;
// return tmp;
//}
Integer &Integer::operator --()
{
--n_;
return *this;
}
//Integer& operator--(Integer& i)
//{
// --i.n_;
// return i;
//}
Integer Integer::operator--(int n)
{
//n_--;
Integer tmp(n_);
n_--;
return tmp;
}
//Integer operator--(Integer& i, int n)
//{
// Integer tmp(i.n_);
// i.n_--;
// return tmp;
//}
void Integer::Display() const
{
cout << n_ << endl;
}
int main(void)
{
Integer n(100);
n.Display();
cout << "++阶段" << endl;
Integer n2 = ++n;
n.Display();
n2.Display();
Integer n3 = n++;
n.Display();
n3.Display();
cout << "--阶段" << endl;
Integer m(100);
Integer m2 = --m;
m.Display();
m2.Display();
Integer m3 = m--;
m.Display();
m3.Display();
return 0;
}
需要注意的是为了区别于前置++,后置++多了一个int 参数,但实际上是没作用的,设置断点调试的时候可以发现默认赋值为0。而且此时成员函数不能与友元函数共存,因为调用++运算符时不明确。
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class CMyString
{
public:
CMyString(const char* pData = NULL);
CMyString(const CMyString& other);
~CMyString();
CMyString& operator =(const CMyString& str);
bool operator !() const;
void display() const;
private:
char* m_pData;
};
CMyString::CMyString(const char* pData)
{
if(pData == NULL)
{
m_pData = new char[1];
*m_pData = '\0';
}
else
{
int length = strlen(pData);
m_pData = new char[length + 1];
strcpy(m_pData, pData);
}
}
CMyString::CMyString(const CMyString& other)
{
int Len = strlen(other.m_pData);
m_pData = new char[Len + 1];
strcpy(m_pData, other.m_pData);
}
CMyString::~CMyString()
{
delete []m_pData;
}
CMyString& CMyString::operator =(const CMyString& str)
{
if(this == &str)
return *this;
delete []m_pData;
m_pData = NULL;
m_pData = new char[strlen(str.m_pData) + 1];
strcpy(m_pData, str.m_pData);
return *this;
}
bool CMyString::operator !() const
{
return strlen(m_pData) != 0;
}
void CMyString::display() const
{
cout << m_pData << endl;
}
int main()
{
CMyString s1("abc");
CMyString s2;
s2 = s1;
s2.display();
s2 = "def";
s2.display();
CMyString s3;
bool noempty;
noempty = !s3;
cout << noempty << endl;
s3 = "abc";
noempty = !s3;
cout << noempty << endl;
return 0;
}
解释:假设有三个对象,str1,str2,str3,在程序中语句str1=str2=str3将不能通过编译。!运算符这里指当字符串不为空时为真。
下列代码中,operator+ 调用了operator+= 的实现;只能将流类运算符重载为友元函数,因为第一个参数是流类引用,不是String 类。
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class String
{
public:
String(const char *str = "");
String(const String &other);
String &operator=(const String &other);
String &operator=(const char *str);
bool operator!() const;
char &operator[](unsigned int index);
const char &operator[](unsigned int index) const;
friend String operator+(const String &s1, const String &s2);
String &operator+=(const String &other);
friend ostream &operator<<(ostream &os, const String &str);
friend istream &operator>>(istream &is, String &str);
~String(void);
void Display() const;
int Length() const;
bool IsEmpty() const;
private:
String &Assign(const char *str);
char *AllocAndCpy(const char *str);
char *str_;
};
String::String(const char *str)
{
str_ = AllocAndCpy(str);
}
String::String(const String &other)
{
str_ = AllocAndCpy(other.str_);
}
String &String::operator=(const String &other)
{
if (this == &other)
return *this;
return Assign(other.str_);
}
String &String::operator=(const char *str)
{
return Assign(str);
}
String &String::Assign(const char *str)
{
delete[] str_;
str_ = AllocAndCpy(str);
return *this;
}
bool String::operator!() const
{
return strlen(str_) != 0;
}
char &String::operator[](unsigned int index)
{
return const_cast<char &>(static_cast<const String &>(*this)[index]);
}
const char &String::operator[](unsigned int index) const
{
return str_[index];
}
String::~String()
{
delete[] str_;
}
char *String::AllocAndCpy(const char *str)
{
int len = strlen(str) + 1;
char *newstr = new char[len];
memset(newstr, 0, len);
strcpy(newstr, str);
return newstr;
}
void String::Display() const
{
cout << str_ << endl;
}
int String::Length() const
{
return strlen(str_);
}
bool String::IsEmpty() const
{
return Length() == 0;
}
String operator+(const String &s1, const String &s2)
{
String str = s1;
str += s2;
return str;
}
String &String::operator+=(const String &other)
{
int len = strlen(str_) + strlen(other.str_) + 1;
char *newstr = new char[len];
memset(newstr, 0, len);
strcpy(newstr, str_);
strcat(newstr, other.str_);
delete[] str_;
str_ = newstr;
return *this;
}
ostream &operator<<(ostream &os, const String &str)
{
os << str.str_;
return os;
}
istream &operator>>(istream &is, String &str)
{
char tmp[1024];
cin >> tmp;
str = tmp;
return is;
}
int main(void)
{
String s1("abcdefg");
char ch = s1[2];
cout << ch << endl;
s1[2] = 'A';
s1.Display();
const String s2("xyzabc");
ch = s2[2];
s2.Display();
String s3 = "xxx";
String s4 = "yyy";
String s5 = s3 + s4;
s5.Display();
String s6 = "aaa" + s3 + "sdfadfa" + "xxxx";
s6.Display();
s3 += s4;
s3.Display();
cout << s3 << endl;
String s7;
cin >> s7;
cout << s7 << endl;
if (!s7.IsEmpty())
cout<<s7.Length()<<endl;
return 0;
}
参考:
C++ primer 第四版
版权声明:本文为博主原创文章,未经博主允许不得转载。
C++ Primer 学习笔记_27_操作符重载与转换(2)--++/--运算符重载、!运算符重载、赋值运算符重载 、String类([]、 +、 += 运算符重载)、>>和<<运算符重载
原文地址:http://blog.csdn.net/keyyuanxin/article/details/47321619