我有一个std::basic_streambuf
子类,它使所有输出都以大写形式编写,如下所示:
class upper_streambuf : public std::streambuf
{
public:
upper_streambuf(std::streambuf &real)
: m_realBuffer(real)
{
}
protected:
virtual int overflow(int c)
{
int uc = std::toupper(c);
m_realBuffer.sputc(uc);
return uc;
}
private:
std::streambuf &m_realBuffer;
};
例如,我像这样使用它(这似乎工作正常):
upper_streambuf buf(*std::cout.rdbuf());
std::ostream ucout(&buf);
ucout << "Hello, world!" << std::endl; // prints "HELLO, WORLD!"
我想要实现的是或多或少的相反,我想从流中读取并将所有输入转换为小写。我有以下内容:
class lower_streambuf : public std::streambuf
{
public:
lower_streambuf(std::streambuf &real)
: m_realBuffer(real)
{
}
protected:
virtual int underflow()
{
return std::tolower(m_realBuffer.sbumpc());
}
private:
std::streambuf &m_realBuffer;
};
但是,当我尝试像这样使用这个时:
lower_streambuf buf(*std::cin.rdbuf());
std::istream lcin(&buf);
std::string line;
std::getline(lcin, line);
结果是分段错误。我是否覆盖或调用了错误的函数?请注意,我在 C++ 方面有点新手。另请注意,我知道我可以完整地阅读输入,并在阅读后将其简单地转换为小写,但这更多是出于学习/学术目的,而不是任何实际用途。