c++操作符重载_12
一。使用C++标准库
1.c++标准库并不是C++语言的一部分
2.C++标准库是由C++语言编写而成的类库和函数的集合
3.C++标准库定义的类和对象都位于STD命名空间中
4.C++标准库的头文件都不带.h后缀
5.C++标准库涵盖了C库的类型
二。操作符对象相加的函数实现
#include <cstdlib> #include <iostream> using namespace std; struct Complex { int a; int b; }; Complex add(const Complex& c1,const Complex& c2) { Complex ret = {0,0}; ret.a = c1.a + c2.a; ret.b = c1.b + c2.b; return ret; } int main(int argc, char *argv[]) { Complex c1 = {1,2}; Complex c2 = {3,4}; Complex c3 = add(c1,c2); cout << c3.a << endl; cout << c3.b << endl; cout << "Press the enter key to continue ..."; cin.get(); return EXIT_SUCCESS; }
三。操作符加号的重载
1.c++中通过operator 关键字可以利用函数扩展操作符
2.operator的本质是通过函数重载可以实现操作符重载
#include <cstdlib> #include <iostream> using namespace std; struct Complex { int a; int b; }; Complex operator+ (const Complex& c1,const Complex& c2) { Complex ret = {0,0}; ret.a = c1.a + c2.a; ret.b = c1.b + c2.b; return ret; } int main(int argc, char *argv[]) { Complex c1 = {1,2}; Complex c2 = {3,4}; Complex c3 = c1 + c2; cout << c3.a << endl; cout << c3.b << endl; cout << "Press the enter key to continue ..."; cin.get(); return EXIT_SUCCESS; }
三。C++类中的友元(重点)
1.private声明使得类的成员不能被外界访问
2.通过 friend 关键字可以例外的开放权限
Complex operator+ (const Complex& c1,const Complex& c2)
就可以访问私有成员。
#include <cstdlib> #include <iostream> using namespace std; class Complex { int a; int b; public: Complex(int a = 0,int b = 0) { this-> a = a; this-> b = b; } int getA() { return a; } int getB() { return b; } friend Complex operator+ (const Complex& c1,const Complex& c2); friend ostream& operator <<(ostream& out, const Complex& c); }; ostream& operator <<(ostream& out, const Complex& c) //函数是重点 { out << c.a << "+" << c.b <<"i"; return out; } Complex operator+ (const Complex& c1,const Complex& c2) { Complex ret = (0,0); ret.a = c1.a + c2.a; ret.b = c1.b + c2.b; return ret; } int main(int argc, char *argv[]) { Complex c1(1,2); Complex c2(3,4); Complex c3 = c1 + c2; operator <<(cout,c1); cout << c3 << endl; cout << "Press the enter key to continue ..."; cin.get(); return EXIT_SUCCESS; }
四。小结
1.操作符重载是C++的最强大特性之一
2.操作符重载的本质是通过函数扩展操作符的语义
3.operator 关键字是操作符重载的关键
4.friend 关键字可以对函数或者类开发访问权限
5.操作符重载遵循函数重载的规则
五。操作符重载
1.当无法修改左操作数的类时,使用全局函数进行重载
2.当=,()和->操作符只能通过成员函数进行重载
六。操作符重载
1.重载符+号
Complex operator+ (const Complex& c1,const Complex& c2) { Complex ret = (0,0); ret.a = c1.a + c2.a; ret.b = c1.b + c2.b; return ret; }
2.重载符<<号
ostream& operator <<(ostream& out, const Complex& c) //函数是重点 { out << c.a << "+" << c.b <<"i"; return out; }
3.重载符()号
int operator() () { int ret = a2; int t = a2; a2 = a2 + a1; a1 = t; return ret; }
4.重载符&&
bool operator&& (const Test& obj) { cout<<"bool operator&& (const Test& obj)"<<endl; return i && obj.i; }
5.重载符==
bool Array::operator== (const Array& obj) { bool ret = true; if( mLength == obj.mLength ) { for(int i=0; i<mLength; i++) { if( mSpace[i] != obj.mSpace[i] ) { ret = false; break; } } } else { ret = false; } return ret; }
6.重载符!=
bool Array::operator!= (const Array& obj) { return !(*this == obj); }