相关知识点
class template (模板) 简介
constructor (ctor, 构造函数)
一个变量或对象的数值的设定有两阶段:
- 初始化(initialization),创建新对象的时候执行
- 赋值(assignments), 对象已经存在,时间点晚一些,效率差一些(对象已经存在,空间已经存在)
构造函数初始化列表赋值: 初始化阶段
大括号函数体内赋值: 赋值阶段(非初始化阶段)
所以对于class中的
object data member
、const data member
、reference data member
等必须放在初始化列表中。
ctor (构造函数) 可以有很多個 – overloading (重载)
constructor (ctor, 构造函数) 被放在 private (单例设计模式)
参数传递 pass by value vs. pass by reference (引用)(to const)
返回值传递:return by value vs. return by reference (to const)
参数和返回值的传递尽量传引用 (是在可以的情况下)
friend(友元)函数:
打破了 C++ 的封装的大门,类的友元函数能直接拿到该类对象的私有数据,而其他的普通函数只能通过接口访问对象的私有数据。
相同class的各个 objects 互为 friends(友员)
class complex {...// 相同class的各个 objects 互为 friends(友员)int func(const complex ¶m) {return param.re + param.im; // 可直接访问 私有数据}private:double re, im;
};
reference(引用) :
本质就是指针(指针常量,不允许被改变,即引用必须初始化,且一旦初始化完成后不可更改,不能再指向其它类型),但是形式很漂亮。
reference 的主要两个应用就是(出于效率考虑):
- 参数传递
- 返回值传递
Q:参数传递,什么时候用引用传递?
A:传递引用的效率和传递指针的效率一样,所传递的是指针的大小(4字节或8字节),所以一般对于内建类型来说:by value 和 by reference 都可,效率相差不大;但是如果要传递结构体或object的话:尽量 by reference,而且,如果不改变所传递的reference,就在 reference前面加上 const。
Q:返回值传递,什么时候传递引用?
A:需要返回或传递出去的东西(object)已经存在空间,且其生命期在此函数之外,即函数执行完后,需要传递出去的东西还存在,那么就可以 return by reference。对于 local object,即其生命期只局限于在该函数执行期,函数执行完后,该 local object 也就死亡了,对于这样的东西,就不能 return by reference。传出者无需知道接收者是否以 reference 形式接收。
Tips:传递参数和返回值传递的时候,首先考虑能否用 reference,能的话就尽量用引用来传递,另外在传递参数的时候,如果不修改传进来的引用,就最好加上 const。
operator overloading(操作符重载)
操作符可重载为以下两种函数:
- 成员函数
- 任何的 non-ststic 成员函数都有一个隐藏的 this 指针
- 非成员函数(global函数)
<<
只能重载为非成员函数(友元或非友元都可)
操作符重載-1, 成員函數(有this)
操作符重載-2, 非成員函數 (無 this)
temp obbject(临时对象):typename();
临时对象没有名称,生命期到下一行就结束,标准库中应用的较多。
complex operator + (const complex &x, const complex &y) {return complex(x.real() + y.real(), x.imag() + y.imag()); // 临时对象
}
Complex
#ifndef __MYCOMPLEX__ #define __MYCOMPLEX__class complex; complex&__doapl (complex* ths, const complex& r); complex&__doami (complex* ths, const complex& r); complex&__doaml (complex* ths, const complex& r);class complex { public:complex (double r = 0, double i = 0): re (r), im (i) { }complex& operator += (const complex&);complex& operator -= (const complex&);complex& operator *= (const complex&);complex& operator /= (const complex&);double real () const { return re; }double imag () const { return im; } private:double re, im;friend complex& __doapl (complex *, const complex&);friend complex& __doami (complex *, const complex&);friend complex& __doaml (complex *, const complex&); };inline complex& __doapl (complex* ths, const complex& r) {ths->re += r.re;ths->im += r.im;return *ths; }inline complex& complex::operator += (const complex& r) {return __doapl (this, r); }inline complex& __doami (complex* ths, const complex& r) {ths->re -= r.re;ths->im -= r.im;return *ths; }inline complex& complex::operator -= (const complex& r) {return __doami (this, r); }inline complex& __doaml (complex* ths, const complex& r) {double f = ths->re * r.re - ths->im * r.im;ths->im = ths->re * r.im + ths->im * r.re;ths->re = f;return *ths; }inline complex& complex::operator *= (const complex& r) {return __doaml (this, r); }inline double imag (const complex& x) {return x.imag (); }inline double real (const complex& x) {return x.real (); }inline complex operator + (const complex& x, const complex& y) {return complex (real (x) + real (y), imag (x) + imag (y)); }inline complex operator + (const complex& x, double y) {return complex (real (x) + y, imag (x)); }inline complex operator + (double x, const complex& y) {return complex (x + real (y), imag (y)); }inline complex operator - (const complex& x, const complex& y) {return complex (real (x) - real (y), imag (x) - imag (y)); }inline complex operator - (const complex& x, double y) {return complex (real (x) - y, imag (x)); }inline complex operator - (double x, const complex& y) {return complex (x - real (y), - imag (y)); }inline complex operator * (const complex& x, const complex& y) {return complex (real (x) * real (y) - imag (x) * imag (y),real (x) * imag (y) + imag (x) * real (y)); }inline complex operator * (const complex& x, double y) {return complex (real (x) * y, imag (x) * y); }inline complex operator * (double x, const complex& y) {return complex (x * real (y), x * imag (y)); }complex operator / (const complex& x, double y) {return complex (real (x) / y, imag (x) / y); }inline complex operator + (const complex& x) {return x; }inline complex operator - (const complex& x) {return complex (-real (x), -imag (x)); }inline bool operator == (const complex& x, const complex& y) {return real (x) == real (y) && imag (x) == imag (y); }inline bool operator == (const complex& x, double y) {return real (x) == y && imag (x) == 0; }inline bool operator == (double x, const complex& y) {return x == real (y) && imag (y) == 0; }inline bool operator != (const complex& x, const complex& y) {return real (x) != real (y) || imag (x) != imag (y); }inline bool operator != (const complex& x, double y) {return real (x) != y || imag (x) != 0; }inline bool operator != (double x, const complex& y) {return x != real (y) || imag (y) != 0; }#include <cmath>inline complex polar (double r, double t) {return complex (r * cos (t), r * sin (t)); }inline complex conj (const complex& x) {return complex (real (x), -imag (x)); }inline double norm (const complex& x) {return real (x) * real (x) + imag (x) * imag (x); }#endif //__MYCOMPLEX__
#include <iostream> #include "complex.h"using namespace std;ostream& operator << (ostream& os, const complex& x) {return os << '(' << real (x) << ',' << imag (x) << ')'; }int main() {complex c1(2, 1);complex c2(4, 0);cout << c1 << endl;cout << c2 << endl;cout << c1+c2 << endl;cout << c1-c2 << endl;cout << c1*c2 << endl;cout << c1 / 2 << endl;cout << conj(c1) << endl;cout << norm(c1) << endl;cout << polar(10,4) << endl;cout << (c1 += c2) << endl;cout << (c1 == c2) << endl;cout << (c1 != c2) << endl;cout << +c2 << endl;cout << -c2 << endl;cout << (c2 - 2) << endl;cout << (5 + c2) << endl;return 0; }