++运算符重载、!运算符重载、赋值运算符重载
2018-02-25 22:59
281 查看
一、++运算符重载
前置++运算符重载
成员函数的方式重载,原型为:
函数类型 & operator++();
友元函数的方式重载,原型为:
friend 函数类型 & operator++(类类型 &);
后置++运算符重载
成员函数的方式重载,原型为:
函数类型 operator++(int);
友元函数的方式重载,原型为:
C++ Codefriend 函数类型 operator++(类类型 &, int);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef _INTEGER_H_ #define _INTEGER_H_ 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); void Display() const; private: int n_; }; #endif // _INTEGER_H_ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include "Integer.h" #include <iostream> using namespace std; Integer::Integer(int n) : n_(n) { } Integer::~Integer() { } Integer &Integer::operator ++() { //cout<<"Integer& Integer::operator ++()"<<endl; ++n_; return *this; } //Integer& operator++(Integer& i) //{ // //cout<<"Integer& operator++(Integer& i)"<<endl; // ++i.n_; // return i; //} Integer Integer::operator++(int n) { //cout<<"Integer& Integer::operator ++()"<<endl; //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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include "Integer.h" #include <iostream> using namespace std; int main(void) { Integer n(100); n.Display(); Integer n2 = ++n; n.Display(); n2.Display(); Integer n3 = n++; n.Display(); n3.Display(); return 0; } |
需要注意的是为了区别于前置++,后置++多了一个int 参数,但实际上是没作用的,设置断点调试的时候可以发现默认赋值为0。
而且此时成员函数不能与友元函数共存,因为调用++运算符时不明确。
二、赋值运算符重载、!运算符重载
C++ Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#ifndef _STRING_H_ #define _STRING_H_ class String { public: explicit String(const char *str = ""); String(const String &other); String &operator=(const String &other); String &operator=(const char *str); bool operator!() const; ~String(void); void Display() const; private: char *AllocAndCpy(const char *str); char *str_; }; #endif // _STRING_H_ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#pragma warning(disable:4996) #include "String.h" #include <string.h> #include <iostream> using namespace std; 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; delete[] str_; str_ = AllocAndCpy(other.str_); return *this; } String &String::operator=(const char *str) { delete[] str_; str_ = AllocAndCpy(str); return *this; } bool String::operator!() const { return strlen(str_) != 0; } 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include "String.h" #include <iostream> using namespace std; int main(void) { String s1("abc"); String s2(s1); String s3; s3 = s1; s3.Display(); s3 = "xxxx"; s3.Display(); String s4; bool notempty; notempty = !s4; cout << notempty << endl; s4 = "aaaa"; notempty = !s4; cout << notempty << endl; return 0; } |
需要注意的是我们将构造函数声明为explicit,故s3 = "xxxx"; 不能将"xxxx" 先隐式转换成临时String再调用 operatorconst String &other);,
可以再重载一个 String& operator=(const char* str); 函数。!运算符这里指当字符串不为空时为真。
参考:
C++ primer 第四版
Effective C++ 3rd
C++编程规范
相关文章推荐
- 055day(赋值运算符的重载和运算符重载为友元函数,例题算24(递归)学习)
- 运算符重载-赋值运算符的重载
- 【C++的探索路7】运算符重载的基本概念与赋值运算符重载
- C++ 重载赋值运算符
- 赋值运算符重载引发的思考(引用的强大功能)
- 关于函数重载,运算符重载一说
- 类的赋值运算符=的重载以及深拷贝和浅拷贝
- [ZZ]重载赋值运算符与拷贝构造函数的区别
- 运算符重载(重载为成员函数)
- C++哪些运算符重载可以重载?
- 详谈面向对象的重载——>“函数重载”与“运算符重载”
- 重载赋值运算符怎么使用的
- c++输入输出重载,赋值,加法运算符重载
- C++构造函数、拷贝构造函数、赋值运算符重载 调用时机 GCC与VC在对象作为返回值的不同处理
- 运算符重载 编程题#2(Coursera 程序设计与算法 专项课程3 C++程序设计;重载:& operator-)
- 1、C++关于拷贝构造函数和赋值运算符重载问题的测试程序。因为调用顺序不清,导致内存泄漏new delete
- C++赋值运算符、函数调用运算符、下标运算符(“=”、“()”、“[]”)重载
- []运算符的重载和二维数组类的[][]运算符重载
- C++本质:类的赋值运算符=的重载,以及深拷贝和浅拷贝(收藏)
- 第九周项目一~~~一目二目运算符重载和<<与>>的重载