c++ 操作符重载
|
可重载 运算符 |
|
||||||||||||||||||
|
不可重载运算符 |
.(成员访问运算符), .*, ->*(成员指针访问运算符) ::(域运算符) , sizeof(长度运算符) ?:(条件运算符), #(预处理符号) |
||||||||||||||||||
|
一元运算符 重载 |
++(自增),--(自减), -(负), !(取反) : 没有参数 #include <iostream> using namespace std; class Distance { private: int feet,inches; public: Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } void displayDistance() { cout << "F: " << feet << " I:" << inches <<endl; } // 重载负运算符( - ) Distanceoperator- (){ feet = -feet; inches = -inches; return Distance(feet, inches); } }; int main(){ Distance D1(11, 10), D2(-5, 11); -D1; // 取相反数 D1.displayDistance(); // 距离 D1 -D2; // 取相反数 D2.displayDistance(); // 距离 D2 return 0; } 运行结果: F: -11 I:-10 F: 5 I:-11 |
||||||||||||||||||
|
二元运算符重载 |
加运算符( + )、减运算符( - )、乘运算符( * )和除运算符( / )都属于二元运算符 一下以加(+)运算符示例。 #include <iostream> using namespace std; class Box{ double length,width,height; public: Box(){} Box(double len, double bre, double hei){ length = len, width=bre; height=hei;} double getVolume(void) { return length * width * height;} // 重载 + 运算符,用于把两个 Box 对象相加 Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.width = this->width + b.width; box.height = this->height + b.height; return box; } }; int main( ){ Box Box1(6.0,7.0,5.0); // 声明 Box1 Box Box2{12.0,13.0,10.0}; // 声明 Box2 Box Box3; // 声明 Box3 cout << "Volume of Box1 : " << Box1.getVolume()<<endl; //Box1 的体积 cout << "Volume of Box2 : " << Box2.getVolume()<<endl; // Box2 的体积 Box3 = Box1 + Box2; // 把两个对象相加,得到 Box3 cout << "Volume of Box3 : " << Box3.getVolume()<<endl; // Box3 的体积 return 0; } 运行结果: Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400 |
||||||||||||||||||
|
关系运算符重载 |
关系运算符( < 、 > 、 <= 、 >= 、 == 等等),它们可用于比较 C++ 内置的数据类型 #include <iostream> using namespace std; class Distance{ private: int feet; // 0 到无穷 int inches; // 0 到 12 public: Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } void displayDistance(){ cout << "F: " << feet << " I:" << inches <<endl; } // 重载负运算符( - ) Distance operator- () { feet = -feet; inches = -inches; return Distance(feet, inches); } // 重载小于运算符( < ) bool operator <(const Distance& d) { if(feet < d.feet) { return true; } if(feet == d.feet && inches < d.inches) { return true; } return false; } }; int main(){ Distance D1(11, 10), D2(5, 11); if( D1 < D2 ) cout << "D1 is less than D2 " << endl; else cout << "D2 is less than D1 " << endl; return 0; } 运行结果: D2 is less than D1 |
||||||||||||||||||
|
输入/输出运算符重载 |
C++ 能够使用流提取运算符 >> 和流插入运算符 << 来输入和输出内置的数据类型。重载流运算符来操作用户自定义的数据类型。注意,有一点很重要,我们需要把运算符重载函数声明为类的友元函数,这样就能不用创建对象而直接调用函数 #include <iostream> using namespace std; class Distance{ private: int feet,int inches; public: Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } friend ostream &operator<<( ostream &output, const Distance &D ) { output << "F : " << D.feet << " I : " << D.inches; return output; } friend istream &operator>>( istream &input, Distance &D ) { input >> D.feet >> D.inches; return input; } }; int main(){ Distance D1(11, 10), D2(5, 11), D3; cout << "Enter the value of object : " << endl; cin >> D3; cout << "First Distance : " << D1 << endl; cout << "Second Distance :" << D2 << endl; cout << "Third Distance :" << D3 << endl; return 0; } 运行结果: $./a.out Enter the value of object : 70 10 First Distance : F : 11 I : 10 Second Distance :F : 5 I : 11 Third Distance :F : 70 I : 10 |
||||||||||||||||||
|
++ 和 -- 运算符重载 |
递增运算符( ++ )和递减运算符( -- )是 C++ 语言中两个重要的一元运算符。 运算符( ++/-- ),包括前缀和后缀两种用法。以下以- -示例,++类同。 #include <iostream> using namespace std; class Check{ private: int i; public: Check(): i(3) { } // 括号无参数为前缀重载 Check operator -- (){ Check temp; temp.i = --i; return temp; } // 括号中插入 int 表示后缀重载 Check operator -- (int){ Check temp; temp.i = i--; return temp; } void Display() { cout << "i = "<< i <<endl; } }; int main(){ Check obj, obj1; obj.Display(); obj1.Display(); // 调用运算符函数,然后将 obj 的值赋给 obj1 obj1 = --obj; obj.Display(); obj1.Display(); // 将 obj 赋值给 obj1, 然后再调用运算符函数 obj1 = obj--; obj.Display(); obj1.Display(); return 0; } 执行输出结果为: i = 3 i = 3 i = 2 i = 2 i = 1 i = 2 |
||||||||||||||||||
|
赋值运算符重载 |
重载赋值运算符( = ),用于创建一个对象,比如拷贝构造函数。 #include <iostream> using namespace std; class Distance{ private: int feet, inches; public: Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } void operator=(const Distance &D ) { feet = D.feet; inches = D.inches; } void displayDistance(){cout << "F: " << feet << " I:" << inches << endl; } }; int main(){ Distance D1(11, 10), D2(5, 11); cout << "First Distance : "; D1.displayDistance(); cout << "Second Distance :"; D2.displayDistance(); D1 = D2; // 使用赋值运算符 cout << "First Distance :"; D1.displayDistance(); return 0; } 运行结果: First Distance : F: 11 I:10 Second Distance :F: 5 I:11 First Distance :F: 5 I:11 |
||||||||||||||||||
|
函数调用运算符 () 重载 |
函数调用运算符 () 可被重载用于类对象。当重载 () 时,创造了一种新的调用函数的方式,相反地,这是创建一个可以传递任意数目参数的运算符函数。 #include <iostream> using namespace std; class Distance{ private: int feet,inches; public: Distance(){ feet = 0; inches = 0; } Distance(int f, int i){ feet = f; inches = i; } // 重载函数调用运算符 Distance operator()(int a, int b, int c) { Distance D; // 进行随机计算 D.feet = a + c + 10; D.inches = b + c + 100 ; return D; } void displayDistance(){ cout << "F: " << feet << " I:" << inches << end; } }; int main(){ Distance D1(11, 10), D2; cout << "First Distance : "; D1.displayDistance(); D2 = D1(10, 10, 10); // invoke operator() cout << "Second Distance :"; D2.displayDistance(); return 0; } 当上面的代码被编译和执行时,它会产生下列结果: First Distance : F: 11 I:10 Second Distance :F: 30 I:120 |
||||||||||||||||||
|
下标运算符 [] 重载 |
下标操作符 [] 通常用于访问数组元素。重载该运算符用于增强操作 C++ 数组的功能。 下面的实例演示了如何重载下标运算符 [] #include <iostream> using namespace std; const int SIZE = 10; class safearay{ private: int arr[SIZE]; public: safearay() { register int i; for(i = 0; i < SIZE; i++) { arr[i] = i; } } int& operator[](int i) { if( i > SIZE ) { cout << "索引超过最大值" <<endl; // 返回第一个元素 return arr[0]; } return arr[i]; } }; int main(){ safearay A; cout << "A[2] 的值为 : " << A[2] <<endl; cout << "A[5] 的值为 : " << A[5]<<endl; cout << "A[12] 的值为 : " << A[12]<<endl; return 0; } 运行结果 A[2] 的值为 : 2 A[5] 的值为 : 5 A[12] 的值为 : 索引超过最大值 0 |
||||||||||||||||||
|
类成员访问运算符 -> 重载 |
类成员访问运算符( -> )被定义用于为一个类赋予"指针"行为。运算符 -> 必须是一个成员函数。如果使用了 -> 运算符,返回类型必须是指针或者是类的对象。运算符 -> 通常与指针引用运算符 * 结合使用,用于实现"智能指针"的功能。这些指针是行为与正常指针相似的对象,唯一不同的是,当您通过指针访问对象时,它们会执行其他的任务。比如,当指针销毁时,或者当指针指向另一个对象时,会自动删除对象。 间接引用运算符 -> 可被定义为一个一元后缀运算符。也就是说,给出一个类: class Ptr{ //... X * operator->(); }; 类 Ptr 的对象可用于访问类 X 的成员,使用方式与指针的用法十分相似。例如:
void f(Ptr p ){ p->m = 10 ; // (p.operator->())->m = 10 } 语句 p->m 被解释为 (p.operator->())->m。同样地,下面的实例演示了如何重载类成员访问运算符 ->。
#include <iostream> #include <vector> using namespace std;
// 假设一个实际的类 class Obj { static int i, j; public: void f() const { cout << i++ << endl; } void g() const { cout << j++ << endl; } };
// 静态成员定义 int Obj::i = 10; int Obj::j = 12;
// 为上面的类实现一个容器 class ObjContainer { vector<Obj*> a; public: void add(Obj* obj) { a.push_back(obj); // 调用向量的标准方法 } friend class SmartPointer; }; // 实现智能指针,用于访问类 Obj 的成员 class SmartPointer { ObjContainer oc; int index; public: SmartPointer(ObjContainer& objc) { oc = objc; index = 0; } // 返回值表示列表结束 bool operator++() { // 前缀版本 if(index >= oc.a.size() - 1) return false; if(oc.a[++index] == 0) return false; return true; } bool operator++(int) {// 后缀版本 return operator++(); } // 重载运算符 -> Obj* operator->() const { if(!oc.a[index]) { cout << "Zero value"; return (Obj*)0; } return oc.a[index]; } }; int main() { const int sz = 10; Obj o[sz]; ObjContainer oc; for(int i = 0; i < sz; i++) { oc.add(&o[i]); } SmartPointer sp(oc); // 创建一个迭代器 do { sp->f(); // 智能指针调用 sp->g(); } while(sp++); return 0; } 运行结果: 10 12 11 13 12 14 13 15 14 16 15 17 16 18 17 19 18 20 19 21 |
||||||||||||||||||
|
其他 快速 查看定义 |
#ifndef _STRING_H_ |

浙公网安备 33010602011771号