Как получить из класса std :: ofstream, чтобы добавить несколько действий перед записью в файл?Другими словами, заменив код типа
int main()
{
std::ofstream file("file.txt");
file << "something" << std::endl;
return 0;
}
на
class MyFile : public std::ofstream
{
MyFile(std::string filename) : std::ofstream(filename) {}
??? operator<< ??? <-- HERE1
{
// 1. do some stuff with the input
// 2. if wanted,
// flush it to the base class operator<<
}
};
class MyFileC
{
private:
std::ofstream intern_os;
public:
MyFileC(std::string filename) : intern_os(filename) {}
MyFileC& operator<<( input ??? <-- HERE2 )
{
// 1. do some stuff with the input
// e.g (a) IF the input is a std::string,
// THEN, if it contains "signal",
// OnSignal();
// or (b) if file size is too big, clear it ...
// 2. Flush it (for all possible types):
intern_os << input;
}
};
int main()
{
MyFile file("file2.txt"); // (or with MyFileC)
file << "something" << std::endl;
return 0;
}
, где мы могли бы, например, фильтровать на лету перед записью и т. Д.
Что поставитьв ??? строках, чтобы насладиться всеми существующими std :: ofstream.operator << (), с нашими личными добавлениями, пожалуйста? </p>
- ЗДЕСЬ1 : более элегантный, с наследственным подходом;или, если это невозможно (после комментариев ниже),
- HERE2 : какой тип передать "всему, что может быть передано на внутренний std :: ofstream" (строки, целые, ...)