我正在使用一个旨在像这样使用的类:
Output() << "Hello.\n";
在其 operator<<
我明确使用 std::cout
,但我想要一个解析为“std::cout”的静态类成员,这样我就可以做这样的事情:
copy(some_string_set.begin(), some_string_set.end(), ostream_iterator<string>(Output::m_stream, ", "));
或类似的东西(在我修复静态数据成员之前我无法修复底线。
我什至试过auto
,但是 GCC 抛出了一个
error: 'std::cout' cannot appear in a constant-expression
对着我。我怎样才能做我想做的事? (关键是不必在我的代码中使用 std::cout
,而是让所有输出都通过 Output 类)
最佳答案
struct Output
{
static ostream& stream;
};
ostream& Output::stream = cout;
int main()
{
Output::stream << "hey";
}
在这里工作正常。
关于c++ - 如何将对 `std::cout` 的引用存储为类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5081794/