---
title: "inherit the ostream"
author: "chenqi210"
date: "2012-10-26T16:02:43Z"
tags: []
categories: ["编程语言"]
slug: "inherit-the-ostream"
draft: false
lastmod: "2012-10-26T16:02:43Z"
id: 1706149
url: "https://chenqi210.iteye.com/blog/1706149"
---

```
class cdebug_stream :public ostream
{
public:
	typedef cdebug_stream& (*manip_fnptr)(cdebug_stream&);
	void attach(ostream& os) { m_outs.push_back(&os); }
	void detach(ostream& os) { m_outs.erase(std::remove(m_outs.begin(),m_outs.end(),&os),m_outs.end()); }
	template<typename T>
	cdebug_stream& operator << (T const& val) {
		for(vector<ostream*>::iterator it = m_outs.begin();
				it != m_outs.end();
				++it)
			(**it) << val;
		return *this;
	}
	cdebug_stream& operator << (ostream& (*manip)(ostream&)) {
		for(vector<ostream*>::iterator it = m_outs.begin();
				it != m_outs.end();
				++it)
			(*manip)(**it);
		return *this; 
	}
private:
	vector<ostream*> m_outs;
} cdebug;
```