Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to correctly derive from ostream class?

Posted on 1998-11-05
5
Medium Priority
?
386 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

636 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