|
⇤ ← 于2006-06-26 19:58:49修订的的版本1
大小: 2768
备注:
|
大小: 5406
备注:
|
| 删除的内容标记成这样。 | 加入的内容标记成这样。 |
| 行号 24: | 行号 24: |
| 运算符重载:为类添加运算符 | 运算符重载:为类型添加运算符 |
| 行号 90: | 行号 91: |
| 单目运算符可以用不带参数的成员函数的形式,也可以用带一个参数的普通函数的形式重载 {{{#!cplusplus class X { X* operator&(); // &a X* operator&(X b); // 双目 a & b X operator++(); // ++a }; X operator-(X); // -a X operator--(X); //--a void f( X a) { &a; // 相当于a.operator&(); -a; //相当于 operator-(a); --a; //相当于 operator--(a); ++a; //相当于 a.operator++(); } }}} 完整的complex运算符 {{{#!cplusplus class complex { double re, im; public: complex( double r = 0, double i = 0): re(r), im (i) { } double& real() { return re; } double& imag() { return im; } complex operator+() { return *this; } complex operator-() { return complex(-re, -im); } complex operator+=( complex c) { re+= c.re; im+= c.im; return *this; } complex operator-=( complex c); complex operator*=( complex c); complex operator/=( complex c); }; complex operator+( complex a, complex b) { a+=b; return a; } complex operator-( complex a, complex b); complex operator*( complex a, complex b); complex operator/( complex a, complex b); }}} 练习:创建分数类,定义尽可能多的运算符 |
|
| 行号 91: | 行号 136: |
| 移位运算符,经常重载作输入输出用 {{{#!cplusplus void f( complex c) { cout << “ hello world” << 100; cout << c ; // operator<<(cout, c) } ostream &operator<<(ostream&os, complex c) { return os << “(” << c.real() << “+” << c.imag() << “i)”; } istream &operator>>(istream&is, complex &c) { return is >> c.real() >> c.imag(); } }}} 练习:输出输出Date类型的值 |
|
| 行号 92: | 行号 151: |
| 赋值运算符,自定义对象赋值行为 {{{ Class complex; complex c1; complex c2(c1); //拷贝构造 c2 = c1; void f( complex c); f( c1 ); //拷贝构造 }}} 如果没有自定义赋值运算符,默认的赋值行为:类中逐个成员赋值。 class string { char *str; int size; public: string( char *s ) { size = strlen(s) +1; str = new char[size]; strcpy(str, s); } ~string( ) { delete [ ] str; } string( const string& c){ size = c.size; str = new char [size]; strcpy(str, c.str); } string& operator=(const string&c){ if(this != &c) { delete [ ] str; size = c.size; str = new char[size]; strcpy(str, c.str); } return *this; } }; int main() { string c ( “123” ); string d ( c ); c = d; // c.operator=(d) } }}} |
C++运算符重载
1. 一般运算符的重载
复数类
1 class complex {
2 double re, im;
3 public:
4 complex(double r, double i);
5 complex add(complex b);
6 complex multiply(complex b);
7 };
8 void f() {
9 complex a(1.0, 1.0), b(-1.0, -2.0), c(0, 0);
10 a = b.add(c);
11 b = b.add(c.multiply(a));
12 c = a.multiply(b).add(complex(1,2));
13 // a = b+c;
14 // b = b + c * a;
15 // c = a * b + complex(1, 2);
16 }
运算符重载:为类型添加运算符
1 class complex {
2 double re, im;
3 public:
4 complex(double r, double i);
5 complex operator+ (complex b);
6 complex operator* (complex b);
7 };
8 int main() {
9 complex a(2, 3), b(-1, 2), c(3, 4);
10 a = b + c; // b.operator+(c)
11 b = b + c * a; // b.operator+(c.operator*(a))
12 c = a * b + complex(1, 2); //a.operator*(b).operator+(complex(1,2));
13 }
14 complex complex::operator+(complex b) {
15 return complex(re + b.re, im + b.im);
16 }
能够重载的运算符
+ - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- , -> [ ] () new new[ ] delete delete[ ]
不能重载的运算符
:: . .* ?: sizeof typeid
重载的运算符是一个名字和调用方法特殊的函数。可以用成员函数的方式重载,也可以用普通函数的方式重载。
complex operator+( complex c, complex d);
重载的运算符的参数至少有一个是类
int operator+(int a, int b); //error bool operator==(char *, char *); // error
[ ], (), =, ->运算符必须以成员函数方式重载。运算符重载不会改变运算符原有的优先级、操作数数目、结合性。
complex a, b, c; a+b*c;
不能够自己创造运算符,比如**。尽量不要改变运算符原来的语义。
二元运算符可以用带一个参数的成员函数的形式,也可以用带两个参数的普通函数的形式重载。区别:成员函数重载,第一个参数只能是类的对象。
单目运算符可以用不带参数的成员函数的形式,也可以用带一个参数的普通函数的形式重载
完整的complex运算符
1 class complex {
2 double re, im;
3 public:
4 complex( double r = 0, double i = 0): re(r), im (i) { }
5 double& real() { return re; }
6 double& imag() { return im; }
7 complex operator+() { return *this; }
8 complex operator-() { return complex(-re, -im); }
9 complex operator+=( complex c) { re+= c.re; im+= c.im; return *this; }
10 complex operator-=( complex c);
11 complex operator*=( complex c);
12 complex operator/=( complex c);
13 };
14
15 complex operator+( complex a, complex b) {
16 a+=b; return a;
17 }
18 complex operator-( complex a, complex b);
19 complex operator*( complex a, complex b);
20 complex operator/( complex a, complex b);
练习:创建分数类,定义尽可能多的运算符
2. operator<<, operator>> 输入输出
移位运算符,经常重载作输入输出用
练习:输出输出Date类型的值
3. operator= 赋值
赋值运算符,自定义对象赋值行为
Class complex; complex c1; complex c2(c1); //拷贝构造 c2 = c1; void f( complex c); f( c1 ); //拷贝构造
如果没有自定义赋值运算符,默认的赋值行为:类中逐个成员赋值。 class string {
- char *str; int size;
public:
- string( char *s ) {
- size = strlen(s) +1; str = new char[size]; strcpy(str, s);
- delete [ ] str;
string( const string& c){
- size = c.size; str = new char [size]; strcpy(str, c.str);
string& operator=(const string&c){
if(this != &c) {
- delete [ ] str; size = c.size; str = new char[size]; strcpy(str, c.str);
};
int main() {
- string c ( “123” ); string d ( c ); c = d; // c.operator=(d)
} }}}
4. operator type 类型转换
5. operator[ ] 数组取成员
6. operator() 函数调用
7. operator++, operator-- 自增自减
8. operator-> 指针取成员