C++学习笔记8 复合

//类与类之间的关系
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构造函数由好几个,我们像特定的调用某一个,就要自己写

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值