468,766 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

problem deriving class from ostringstream (operator <<)

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::ostringstream;
using std::ostream;
using std::ofstream;
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 ?
Jul 22 '05 #1
2 5334
On Mon, 13 Dec 2004 13:35:51 -0600, "Julian" <ju****@nospamtamu.edu>
wrote:
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::ostream 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::ostringstream;
using std::ostream;
using std::ofstream;
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/

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2
See
http://groups-beta.google.com/group/...639513a8f7406b
for all output operators that you need in IOM.
If you can C&P search c.l.c++ for "Efficient 'logging' mechanism?", you
should then get the same message.

Stephan

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by lutorm | last post: by
5 posts views Thread by Karl | last post: by
5 posts views Thread by krzysztof.konopko | last post: by
3 posts views Thread by subramanian100in | last post: by
4 posts views Thread by =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.