• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

How to correctly derive from ostream class?

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
danchandran
Asked:
danchandran
  • 3
  • 2
1 Solution
 
nietodCommented:
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
 
nietodCommented:
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
 
danchandranAuthor Commented:

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
 
nietodCommented:
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
 
danchandranAuthor Commented:
Thanks,

I have almost a similar implementation working!!

daniel
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now