Extending ofstream class?

L

Lars Yencken

Hello,

I'm working on a project where we need to index files as we're writing them,
which basically just involves determining how many bytes are output each
time we write to a stream.

What I've been trying is essentially the code below. It seg. faults though
when I try it out. There are some obvious deficiencies to me:
- I shouldn't have to have a private ofstream attribute, I should be able to
use the inherited constructor and methods to do the same job.
- It seems like sometimes it uses the template << operator that I defined,
and at other times it uses the inherited operator, though I'm not sure why.

Has anyone tried to do something like this before, or can someone please
give me some advice as to what I'm doing wrong?

Thanks,
Lars

IndexedStream.h
--------------------------------------------------------------------------

#include <fstream>
#include <string>

using namespace std;

class IndexedStream : public ofstream
{
public:
IndexedStream( string filename );

template< class T >
IndexedStream& operator<<( const T& value );

private:
ofstream m_outfile;
int m_offset;
}

template< class T >
IndexedStream& IndexedStream::eek:perator<<( const T& value )
{
m_outfile << value;
doSomethingWith( (int) m_outFile.tellp() - m_offset );
}


IndexedStream.cpp
---------------------------------------------------------------------------

#include "IndexedStream.h"

IndexedStream::IndexedStream( string filename )
{
m_outfile.open( filename.c_str(), ios::eek:ut );
}
 
J

John Harrison

Lars Yencken said:
Hello,

I'm working on a project where we need to index files as we're writing them,
which basically just involves determining how many bytes are output each
time we write to a stream.

What I've been trying is essentially the code below. It seg. faults though
when I try it out. There are some obvious deficiencies to me:
- I shouldn't have to have a private ofstream attribute, I should be able to
use the inherited constructor and methods to do the same job.

I disagree, you need a private attribute, in this case inheriting is wrong.
- It seems like sometimes it uses the template << operator that I defined,
and at other times it uses the inherited operator, though I'm not sure
why.

Right, operator<< is not virtual, so this method is never going to work
reliably.
Has anyone tried to do something like this before, or can someone please
give me some advice as to what I'm doing wrong?

You should not try to force fit your indexed scheme into the usual iostream,
its too different . This means you should not inherit from ofstream.

When make this change it should all become a lot easier.

john
 
L

Lars Yencken

John said:
You should not try to force fit your indexed scheme into the usual
iostream, its too different . This means you should not inherit from
ofstream.

When make this change it should all become a lot easier.

john

Thanks John. I've tried it out and it's actually working nicely. The main
annoyance with this method is that I can't seem to write endl to my class;
I get compile errors when I do. It can easily be worked around by writing
just the string for newline, but it'd be nice if there was a way to do this
too, especially since it'd prevent me having to modify a lot of other code.
I'm guessing that if writing endl doesn't work, stream modifiers in general
won't work either.

Any suggestions on this?

Lars
 
Joined
Mar 18, 2008
Messages
1
Reaction score
0
To answer your question (from 2003) but might help someone else, you need to add something like this so your code above can handle processing the endl to your << operator:

Code:
    // this is the type of std::cout
    typedef std::basic_ostream< char, std::char_traits<char> > CoutType;

    // this is the function signature of std::endl
    typedef CoutType & (*StandardEndLine)(CoutType&);

    // define an operator<< to take in std::endl
    IndexedStream& operator << (StandardEndLine manip) {
        m_outfile  << endl;
        return *this;
    }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top