std::ofstream 中的自定义流缓冲区

Custom streambuffer in std::ofstream

我知道在 std::ostream 中,我可以通过在构造函数中这样声明来使用自定义 streambuf

std::ofstream temp;
temp.open("file.txt", std::ios_base::in);
std::ostream example(temp.rdbuf());

以及之后设置(前两行与之前相同,但将最后一行更改为:

std::ostream example;
example.rdbuf(temp.rdbuf());

我的问题是:如何在 std::ofstream 中做到这一点?我希望能够在我自己的自定义 class 中覆盖在 std::streambuf 中实现的方法 xsgetnxsputn,并在我的 ofstream 中使用它,但是,简而言之写我自己的习惯 ofstream 我不确定该怎么做。

具体文件流 classes 有自己的 rdbuf() 方法,该方法接受 0 个参数,它 隐藏 继承的另一个 rdbuf() 方法来自虚拟基地 std::basic_ios。限定名称以查找基本 class 方法应该有效:

std::ofstream ofs;
ofs.basic_ios<char>::rdbuf(example.rdbuf());