473,761 Members | 7,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::ostringstr eam;
using std::ostream;
using std::ofstream;
using std::cout;

class IOM : public ostringstream {
public:
IOM(){Log.open( "log.txt"); };
~IOM(){Log.clos e();};
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 5680
On Mon, 13 Dec 2004 13:35:51 -0600, "Julian" <ju****@nospamt amu.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::s tring 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::ostringstr eam;
using std::ostream;
using std::ofstream;
using std::cout;

class IOM : public ostringstream {
public:
IOM(){Log.open( "log.txt"); };
~IOM(){Log.clos e();};
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**********@Ho me.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2339
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> using namespace std;
5
5699
by: Karl | last post by:
Hey everyone! So I'm writing my own String class to wrap std::string and implement an API as close to identical as possible to the Java API. It's pretty small so far: #include <string> class String {
17
2331
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks << endl;
5
3658
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I define map type with my new type (structure) everything is OK. All compileres I've cheked report an error so I think it is a problem with my code. #include <algorithm> #include <cstddef>
3
1884
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
1
1416
by: Stuart Golodetz | last post by:
Hi guys, I'm trying to making an instance of a templated operator<< for a templated class a friend of that class (see below), to allow it to access the class internals for output purposes. #include <iostream> template <typename T> class TC
4
1894
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m); };
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.