?
Solved

How to correctly derive from ostream class?

Posted on 1998-11-05
5
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 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

Industry Leaders: 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!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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