c++11 标准模板(STL)(std::basic_iostream)(二)

定义于头文件 <istream>
template<

    class CharT,
    class Traits = std::char_traits<CharT>

> class basic_iostream;

类模板 basic_iostream 提供流上的高层输入/输出支持。受支持操作包含读或写及格式化。此功能为 basic_streambuf 类所提供,在接口上实现。通过 basic_ios 类访问缓冲区。

继承图

为常用字符类型定义二个特化:

定义于头文件 <istream>

类型 定义
iostream basic_iostream<char>
wiostream basic_iostream<wchar_t>

成员函数

构造对象

std::basic_iostream<CharT,Traits>::basic_iostream

explicit basic_iostream( std::basic_streambuf<CharT,Traits>* sb );

(1)

basic_iostream( const basic_iostream& other ) = delete;

扫描二维码关注公众号,回复: 15783341 查看本文章
(2) (C++11 起)

protected:
basic_iostream( basic_iostream&& other );

(3) (C++11 起)

构造新的流对象。

1) 以 streambuf sb 初始化。基类初始化为 basic_istream<CharT,Traits>(sb) 和 basic_ostream<CharT,Traits>(sb) 。调用后 rdbuf() == sb 而 gcount() == 0 。

2) 不允许复制构造。

3) 移动构造函数:移动构造第一基类 basic_istream 为 basic_istream<CharT,Traits>(std::move(rhs)); ,这会继续移动构造并初始化虚基类 std::basic_ios 。另一基类 basic_ostream 的初始化是实现定义的(例如,可以添加不做任何事的受保护默认构造函数到 std::basic_ostream ),因为移动构造不能使用 rhs 二次。此移动构造函数受保护:它为导出流类 std::basic_fstream 和 std::basic_stringstream 的移动构造函数在它们移动并关联流缓冲前调用。

参数

sb - 用以初始化的 streambuf
other - 用以初始化的另一流

析构对象

std::basic_iostream<CharT,Traits>::~basic_iostream

virtual ~basic_iostream();

析构输入/输出流。

注意

此析构函数不在底层流缓冲( rdbuf() )上进行任何操作:导出流,如 std::basic_fstream 和 std::basic_stringstream 的析构函数负责调用流缓冲的析构函数。

受保护成员函数

移动赋值另一 basic_iostream

std::basic_iostream<CharT,Traits>::operator=

basic_iostream& operator=( const basic_iostream& other ) = delete;

(1)

protected:
basic_iostream& operator=( basic_iostream&& other );

(2) (C++11 起)

赋值另一流对象。

1) 不允许复制赋值。

2) 移动赋值另一流对象。等效地调用 swap(rhs) 。此移动赋值运算符受保护:它为导出流类 std::basic_stringstream 和 std::basic_fstream 的移动赋值运算符所调用,这些运算符知晓如何正确地移动赋值关联的流缓冲。

参数

other - 要赋值其状态的另一流

返回值

*this

与另一 basic_iostream 交换状态

std::basic_iostream<CharT,Traits>::swap

protected:
void swap( basic_iostream& other );

(C++11 起)

与另一输入/输出流交换状态。等效地调用 basic_istream<CharT,Traits>::swap(other) 。

此成员函数受保护:导出流类 std::basic_stringstream 和 std::basic_fstream 的 swap 成员函数知晓如何正确交换关联缓冲区,并调用此函数。

参数

other - 要交换状态的另一流

返回值

*this

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/131737058