Solved

How to correctly derive from ostream class?

Posted on 1998-11-05
5
351 Views
Last Modified: 2008-02-01
I am a novice C++ programmer, and this is very basic.

I would like define a Log class derived from ostream,
so that in it's simple implemenation it would just print
to cout, and in the later version it would write to a file.
I am looking for something like as follows.

#include <ostream.h>

class Log : ostream
{
public:
  ...
  ..

}

main(int argc, char *argv)
{

  log Log;

  log << "Logging data" << endl;

}

Thanks,
daniel
0
Comment
Question by:danchandran
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1177046
To derive from ostream for your purposes you woul want to use pubic derivation, like

class Log : public ostream
 {
   public:
     ...
     ..

 }

then you would overide any member procedures you would like changed.  Unfortunately, considering what you are proposing there are many such procedures you might need to change.  (all the operator << procedures) for example.   This could be a lot of work.  I suspect there might be a better way to accomplish your goals, except I'm not sure what your goal is.  What exactly are you hoping to achieve?


0
 
LVL 22

Expert Comment

by:nietod
ID: 1177047
One possibility might be a log class that is not derived from a stream, but contains a stream pointer and has a "ostream &" operator that returns the dereferenced stream pointer.  That might work well for your needs.  
0
 

Author Comment

by:danchandran
ID: 1177048

I am looking way of this implementation of the Log Class

class Log :
{
   public:
          Log();
          ~Log();
        Write(int);
        Write(char *);
        Write(float);
        ....

}

I have a whole bunch of overloaded Write methods, and I was hoping that by doing
something like what I have asked, I could get around it.

Thanks,
daniel
0
 
LVL 22

Expert Comment

by:nietod
ID: 1177049
Then my final suggestion should work well for you--I think.

1.  Make the Log class contain a pointer to the stream it should output to.  This pointer should be set by the constructor (and/or default to cout)
2.  Remove the Write() functions.
3.   Add a stream & conversion operator so that the class can be used wherever a stream would be used.  

you should have something like

class Log :
{
   ostream *StmPtr;
public:
   Log(stream *Ptr = &cout) : StmPtr(Ptr) {};
    ~Log();
   SetStmPtr(ostream *Ptr = &cout) { StmPtr = Ptr; };
   operator ostream & () { return *StmPtr; };
 }

this should allow yout to do things like

Log ALog;
ofstream FilStm("C:\\LOG.TXT");

ALog << " output to cout. " << endl;
ALog.SetStmPtr(&FilStm);
ALog << " output to a file. " << endl;

0
 

Author Comment

by:danchandran
ID: 1177050
Thanks,

I have almost a similar implementation working!!

daniel
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now