By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Extending ofstream class?

P: n/a
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::operator<<( 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::out );
}

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Lars Yencken" <ll**@students.cs.mu.oz.au> wrote in message
news:3f******@duster.adelaide.on.net...
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
Jul 19 '05 #2

P: n/a
John Harrison wrote:
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
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.