problem deriving class from ostringstream (operator <<)

J

Julian

I would like to have output from my program to be written to cout as well as
a file. (actually, i want several other output options but this should
explain my problem in the simplest way). I have seen commercial programs
print output to the screen as well as to a log file.
depending on the user and other situations, i might want to turn off one of
the outputs or maybe even both outputs.
so, i want a single line with operator << function calls that can output to
a number of ofstreams/ostringstreams etc. and implement the 'turning off/on'
by settting flags.

this is basically how my program works right now (for outputting to a SINGLE
stream at a time):
#define OUT cout
or
#define OUT (*outStream) // where outStream is a ostream to some file
....
OUT << "this is a test\n";

this is how i was planning on going about it :
deriving a class from ostringstream and overriding the operator << function.
the following sample code writes the 1st string to both cout as well as the
log file but...
1. it doesn't output the 2nd string to either file or cout.
2. and it crashes !! and i am not able to figure out why

#include <iostream>
#include <fstream>
#include <sstream>

using std::eek:stringstream;
using std::eek:stream;
using std::eek:fstream;
using std::cout;

class IOM : public ostringstream {
public:
IOM(){Log.open("log.txt");};
~IOM(){Log.close();};
friend IOM& operator<<(IOM& _O, const char *_X);
ofstream Log;
private:
};

IOM& operator<<(IOM& _O, const char *_X)
{
(ostringstream)_O << _X;
cout << _O.str();
_O.Log << _O.str();
return _O;
}

int main(int argc, char **argv)
{
IOM iom;
iom << "this is" << " a test";
return 0;
}

another question i had was the operator function written above handles only
char*. so, what if i had to pass an integer or even a user-defined class?
do i have to write override functions for every type ?
i am not hell bent on using this method. if someone knows of a better way to
implement this (that is, write to two ofstreams in one operator << function
call), please let me know...


also, on a somewhat different topic, i was able to change the above code to
#include <iostream.h> //as opposed to #include <iostream>
#include <fstream.h> //as opposed to #include <fstream>
but it did not accept
#include <sstream.h>
can someone explain why ?
and when i used just the:
#include <iostream.h>
#include <fstream.h>
i did not have to use the std:: scope resolver.
but when i used it along with
#include <sstream>
it came up with 'multiple declaration' problems
any someone explain this to me or point me to good site/link ?
 
B

Bob Hairgrove

I would like to have output from my program to be written to cout as well as
a file. (actually, i want several other output options but this should
explain my problem in the simplest way). I have seen commercial programs
print output to the screen as well as to a log file.
depending on the user and other situations, i might want to turn off one of
the outputs or maybe even both outputs.
so, i want a single line with operator << function calls that can output to
a number of ofstreams/ostringstreams etc. and implement the 'turning off/on'
by settting flags.

Here's how this is usually done. For simplification, I will assume for
now that the data you are outputting is passed as a std::string ... we
can handle the other types later.

1. Define an abstract base class (called, for example, "Logger") with
a pure virtual function, for example, writeLog(std::string const &).

2. Derive various concrete classes which implement the writeLog()
method. You can have a FileLogger, a DbLogger, and a ConsoleLogger,
for example. FileLogger writes to the file system, DbLogger does a
database update, and ConsoleLogger writes to stdout. You don't
necessarily need std::eek:stream for implementing the writeLog() function
-- it makes sense for FileLogger and ConsoleLogger, but probably not
for DbLogger. Therefore, I wouldn't inherit from ostream at all.

3. Define a class or subsystem in your application which manages the
output. This class, or subsystem, would keep a vector of pointers to
base class Logger. When it comes time to log your data, just iterate
through the vector and call the writeLog() function on your
polymorphic pointer.
this is basically how my program works right now (for outputting to a SINGLE
stream at a time):
#define OUT cout
or
#define OUT (*outStream) // where outStream is a ostream to some file
...
OUT << "this is a test\n";

It is a very bad habit to use macros for this sort of thing. What you
REALLY want is an Abstract Factory (google for "design patterns").
this is how i was planning on going about it :
deriving a class from ostringstream and overriding the operator << function.
the following sample code writes the 1st string to both cout as well as the
log file but...
1. it doesn't output the 2nd string to either file or cout.
2. and it crashes !! and i am not able to figure out why

#include <iostream>
#include <fstream>
#include <sstream>

using std::eek:stringstream;
using std::eek:stream;
using std::eek:fstream;
using std::cout;

class IOM : public ostringstream {
public:
IOM(){Log.open("log.txt");};
~IOM(){Log.close();};
friend IOM& operator<<(IOM& _O, const char *_X);
ofstream Log;
private:
};

Do not use leading underscores in identifiers ... these are "reserved
for the implementation" (i.e. the compiler and all the standard
libraries).
IOM& operator<<(IOM& _O, const char *_X)
{
(ostringstream)_O << _X;
cout << _O.str();
_O.Log << _O.str();
return _O;
}

int main(int argc, char **argv)
{
IOM iom;
iom << "this is" << " a test";
return 0;
}

If you never use argc and argv, why include them at all?
another question i had was the operator function written above handles only
char*. so, what if i had to pass an integer or even a user-defined class?
do i have to write override functions for every type ?

Not necessarily, you could define a template class and log any type
you wish.

BTW it's overload, not override: overloading is when different
functions with the same name, often in the same class, take different
argument types and/or number of arguments, or have different
const-ness.

Overriding is writing two functions with the same name AND the same
arguments and number of arguments, one in a derived class which
inherits from a base class containing the other function which must be
declared as a virtual function in the base class.
i am not hell bent on using this method. if someone knows of a better way to
implement this (that is, write to two ofstreams in one operator << function
call), please let me know...


also, on a somewhat different topic, i was able to change the above code to
#include <iostream.h> //as opposed to #include <iostream>
#include <fstream.h> //as opposed to #include <fstream>

You shouldn't use these old headers (ending in *.h) which are there
only for backwards compatibility with code which uses them. It is
dangerous to mix the newer headers with the older ones exactly because
of problems such as the ones you describe below.
but it did not accept
#include <sstream.h>
can someone explain why ?

sstream is newer than the old headers which end with .h, so there
isn't one.
and when i used just the:
#include <iostream.h>
#include <fstream.h>
i did not have to use the std:: scope resolver.
but when i used it along with
#include <sstream>
it came up with 'multiple declaration' problems
any someone explain this to me or point me to good site/link ?

Yes:
http://www.parashift.com/c++-faq-lite/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top