You can accomplish that by using a helper class and functions to go with it.
// The class
struct my_out
{
my_out(std::ostream& out1, std::ostream& out2) : out1_(out1), out2_(out2) {}
std::ostream& out1_;
std::ostream& out2_;
};
// operator<<() function for most data types.
template <typename T>
my_out& operator<<(my_out& mo, T const& t)
{
mo.out1_ << t;
mo.out2_ << t;
return mo;
}
// Allow for std::endl to be used with a my_out
my_out& operator<<(my_out& mo, std::ostream&(*f)(std::ostream&))
{
mo.out1_ << f;
mo.out2_ << f;
return mo;
}
You'll have to add similar helper functions to deal with objects from <iomanip>
.
Use it as:
std::ofstream helloworld;
helloworld.open("helloworld.txt");
my_out mo(std::cout, hellowworld);
string hello ="welcome";
mo << hello << std::endl;