にパラメータを追加することはできませんが、詳細パラメータoperator<<
を使用してカスタム印刷関数を簡単に定義できます。
void dump(ostream& ostr, const myClass& obj, int verbosity = 1)
{
if (verbosity > 2)
ostr << "Very verbose!\n";
if (verbosity > 1)
ostr << "Verbose!\n";
if (verbosity > 0)
ostr << "Standard!\n";
ostr << "Minimal.\n";
}
使用法:
dump(cout, myobj); // Default
dump(cout, myobj, 0); // Minimal
dump(cout, myobj, 1); // Default
dump(cout, myobj, 2); // Verbose
dump(cout, myobj, 3); // Very verbose
dump()
また、デフォルトの詳細度を使用して、に転送するストリーム演算子を提供する必要があります。
ostream& operator<<(ostream& ostr, const myClass& obj)
{
dump(ostr, obj);
return ostr;
}
そのようにしたい場合は、senum
を使用する代わりに冗長性を宣言することをお勧めします。int
enum Verbosity
{
MinimalOutput = 0,
StandardOutput = 1,
VerboseOutput = 2,
DebugOutput = 3
};