ostream 和 fstream 的超类
Superclass for ostream and fstream
我正在尝试使用包装器 class 在 C++ 中创建自己的日志记录 class,其中我重载了 operator<< 并将其发送到 cout。现在我想改变它,这样当我创建那个 class 的实例时,我可以传递和论证在 std::cout 或我创建的某个文件中记录数据。 fstream 和 ostream 的 superclass 的确切类型是什么?我尝试使用 std::ios&、std::basic_ios&、std::basic_ostream& 和 none 似乎有效(抛出编译错误)。
class myostream {
public:
static int getlogLevel() {
return loglevel;
}
static void setlogLevel(int i) {
loglevel = i;
}
myostream(std::basic_ios& cout, int level)
: _cout(cout), _level(level)
{}
template<class T>
std::ostream& operator<<(T t) {
if(_level >= loglevel) {
_cout << loglevelcolor[_level] << loglevelname[_level] << " " << t << COL_RESET << std::endl;
}
return _cout;
}
private:
static int loglevel;
std::basic_ostream& _cout;
int _level;
};
What is the exact type that is superclass of both fstream
and ostream
?
是std::ostream
,是std::basic_ostream<char>
的别名。请参见 std::fstream
的 class 图。
示例:
class myostream {
public:
myostream(int level) // Log into stdout.
: _cout(std::cout), _level(level)
{}
myostream(char const* filename, int level) // Log into a file.
: _file(filename), _cout(_file), _level(level)
{
if(!_file.is_open())
throw std::runtime_error("Failed to open " + std::string(filename));
}
// ...
private:
std::ofstream _file;
std::ostream& _cout;
int _level;
};
使用基础 class std::ostream
which is typedef for basic_ostream<char>
, reference: iostream hierarchy.
对我有用(std::cout,std::ofstream):
#include <iostream>
class myostream {
public:
myostream(std::ostream& out)
: _out(out) {}
template<class T>
std::ostream& operator<<(T t) {
_out << "test" << " " << t << '\n' << 42 << std::endl;
return _out;
}
private:
std::ostream& _out;
};
我正在尝试使用包装器 class 在 C++ 中创建自己的日志记录 class,其中我重载了 operator<< 并将其发送到 cout。现在我想改变它,这样当我创建那个 class 的实例时,我可以传递和论证在 std::cout 或我创建的某个文件中记录数据。 fstream 和 ostream 的 superclass 的确切类型是什么?我尝试使用 std::ios&、std::basic_ios&、std::basic_ostream& 和 none 似乎有效(抛出编译错误)。
class myostream {
public:
static int getlogLevel() {
return loglevel;
}
static void setlogLevel(int i) {
loglevel = i;
}
myostream(std::basic_ios& cout, int level)
: _cout(cout), _level(level)
{}
template<class T>
std::ostream& operator<<(T t) {
if(_level >= loglevel) {
_cout << loglevelcolor[_level] << loglevelname[_level] << " " << t << COL_RESET << std::endl;
}
return _cout;
}
private:
static int loglevel;
std::basic_ostream& _cout;
int _level;
};
What is the exact type that is superclass of both
fstream
andostream
?
是std::ostream
,是std::basic_ostream<char>
的别名。请参见 std::fstream
的 class 图。
示例:
class myostream {
public:
myostream(int level) // Log into stdout.
: _cout(std::cout), _level(level)
{}
myostream(char const* filename, int level) // Log into a file.
: _file(filename), _cout(_file), _level(level)
{
if(!_file.is_open())
throw std::runtime_error("Failed to open " + std::string(filename));
}
// ...
private:
std::ofstream _file;
std::ostream& _cout;
int _level;
};
使用基础 class std::ostream
which is typedef for basic_ostream<char>
, reference: iostream hierarchy.
对我有用(std::cout,std::ofstream):
#include <iostream>
class myostream {
public:
myostream(std::ostream& out)
: _out(out) {}
template<class T>
std::ostream& operator<<(T t) {
_out << "test" << " " << t << '\n' << 42 << std::endl;
return _out;
}
private:
std::ostream& _out;
};