Solved

How to correctly derive from ostream class?

Posted on 1998-11-05
5
350 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

16 Experts available now in Live!

Get 1:1 Help Now