gpt4 book ai didi

c++ - 为什么将 std::basic_streambuf* 插入 ostream 会插入缓冲区内容?

转载 作者:行者123 更新时间:2023-11-30 04:15:34 24 4
gpt4 key购买 nike

以这个简单的示例程序为例:

// main.cpp
#include <iostream>
#include <fstream>


int main(int argc, const char *argv[])
{
using namespace std;
fstream infile("main.cpp");
basic_streambuf<char> *buf = infile.rdbuf();
cout << static_cast<void *> (buf) << endl;
cout << buf;
}

为了打印basic_streambuf<>的实际地址object 我必须明确地将它转换为 void * .所以主要问题是,为什么 C++ 对待 basic_streambuf<>就像是某种 const char * ?是否发生了某种隐式转换或者这是什么黑巫术?

检查常见的在线引用资料,例如 cplusplusen.cppreference没有显示 std::basic_streambuf提供任何公共(public)转换运算符。我忽略了什么吗?

最佳答案

http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

ostream& operator<< (streambuf* sb );

Retrieves as many characters as possible from the input sequence controlled by the stream buffer object pointed by sb (if any) and inserts them into the stream, until either the input sequence is exhausted or the function fails to insert into the stream.

http://www.cplusplus.com/reference/streambuf/streambuf/

It is an instantiation of basic_streambuf with the following template parameters: charT = char

关于c++ - 为什么将 std::basic_streambuf* 插入 ostream 会插入缓冲区内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com