null ostream? [英] null ostream?

查看:75
本文介绍了null ostream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何定义继承公共std :: ostream的空ostream和

忽略否则会输出的任何内容?

How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?

推荐答案

Angel Tsankov写道:
Angel Tsankov wrote:
如何定义一个公共std :: ostream继承的空ostream,
忽略任何原本会被输出的东西?
How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?




你可以创建一个写入/ dev / null的ofstream

不可移植,但很容易:)



You could create an ofstream that writes to /dev/null
Not portable, but easy :)


Angel Tsankov写道:
Angel Tsankov wrote:
如何定义一个公共std :: ostream继承的null ostream,
忽略否则会输出的任何东西?
How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output?




我多年前从clc ++中抄袭了这个 - 不记得最初的

作者(但不是我):


#include< streambuf>

#include< ostream>


template< class cT,阶级特征= std :: char_traits< cT> >

class basic_nullbuf:public std :: basic_streambuf< cT,traits> {

typename traits :: int_type overflow(typename traits :: int_type c)

{

return traits :: not_eof(c); //表示成功

}

};


模板< class cT,class traits = std :: char_traits< cT> ; >

class basic_onullstream:public std :: basic_ostream< cT,traits> {

public:

basic_onullstream():

std :: basic_ios< cT,traits>(& m_sbuf),

std :: basic_ostream< cT,traits>(& m_sbuf)

{

init(& m_sbuf);

私有:
basic_nullbuf< cT,traits> m_sbuf;

};


typedef basic_onullstream< char> onullstream;

typedef basic_onullstream< wchar_t> wonullstream;


祝你好运,


Tom



I cribbed this from c.l.c++ years ago - don''t remember the original
author (but it''s not me):

#include <streambuf>
#include <ostream>

template <class cT, class traits = std::char_traits<cT> >
class basic_nullbuf: public std::basic_streambuf<cT, traits> {
typename traits::int_type overflow(typename traits::int_type c)
{
return traits::not_eof(c); // indicate success
}
};

template <class cT, class traits = std::char_traits<cT> >
class basic_onullstream: public std::basic_ostream<cT, traits> {
public:
basic_onullstream():
std::basic_ios<cT, traits>(&m_sbuf),
std::basic_ostream<cT, traits>(&m_sbuf)
{
init(&m_sbuf);
}

private:
basic_nullbuf<cT, traits> m_sbuf;
};

typedef basic_onullstream<char> onullstream;
typedef basic_onullstream<wchar_t> wonullstream;

Best regards,

Tom


试试这个:


#include< iostream>


struct null_streambuf

:public std :: streambuf

{

无效溢出(char c)

{

}

};


int main()

{

null_streambuf nullbuf;


//替换现有流的缓冲区

std :: streambuf * origbuf = std :: cout.rdbuf(& nullbuf);

std :: cout<< 这会被遗忘;

std :: cout.rdbuf(origbuf);


//创建null ostream

std :: ostream out(& nullbuf);

out<< 这也被遗忘......;

}

- 彼得

try this:

#include <iostream>

struct null_streambuf
: public std::streambuf
{
void overflow(char c)
{
}
};

int main()
{
null_streambuf nullbuf;

// replace buffer of existing stream
std::streambuf * origbuf = std::cout.rdbuf(&nullbuf);
std::cout << "this goes into oblivion";
std::cout.rdbuf(origbuf);

// create null ostream
std::ostream out(&nullbuf);
out << "this goes into oblivion too...";
}
-- peter


这篇关于null ostream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆