//类与类之间的关系
inheritance 继承
composition 复合
delegation 委托
//复合
template <class T>
class queue{
...
protected:
deque<T> c;
//底层容器
//复合:我的里面有另外的东西,我和这个另外的东西的关系就是复合
//也就是queue内容纳了deque
public:
//一下完全用c的操作函数完成
bool empty() const {return c.empty();}
size_type size() const {return c.size();}
reference front() {return c.front();}
reference back() {return c.back();}
void push(const value_type& x) {c.push_back(x);}
void pop() {c.pop_front();}
};
Container就是queue,Component就是deque.
//Compositon(复合)关系下的构造和析构
构造由内而外
Container的构造函数首先调用Component的default构造函数,然后才执行自己
Container::Container(...): [Component()] {...};
析构由外而内
Container的析构 函数首先执行自己,然后才调用Component的析构函数
Container::~Container(...) {...[~Component()] }; 删除时,一层一层删
//[ ]内的部分不用我们写,编译器会补全,但如果Component构造函数由好几个,我们像特定的调用某一个,就要自己写