stream constructors and derived classes

T

tarmat

Hi guys,

I have a base class, something like this:

class Base
{
private:

int m_Identifier;

protected:

Base(int id):m_Identifier(id){}

public:

virtual ~Base(){}

};

All the other objects in my project are derived from this class. I'm
trying to create stream constructors for them. Such as

class Derived : public Base
{
public:

Derived(std::ifstream& in);

}

The first variable of the stream is the identifying number, but I
don't know how to write the ctor of Derived to accomodate this. I want
to be able to do the equivalent of the following (which is obviously
not legal)

Derived::Derived(std::ifstream& in):Base(in >>)
{}

How do I get around this problem *without* changing the Base class?

thanks for any help
 
R

Rob Williscroft

tarmat wrote in
[snip]
class Derived : public Base
{
public:

Derived(std::ifstream& in);

}

The first variable of the stream is the identifying number, but I
don't know how to write the ctor of Derived to accomodate this. I want
to be able to do the equivalent of the following (which is obviously
not legal)

Derived::Derived(std::ifstream& in):Base(in >>)
{}

How do I get around this problem *without* changing the Base class?

#include <iostream>
#include <istream>
#include <sstream>
#include <stdexcept>

int istream_get( std::istream &is )
{
int nrv;
is >> nrv;

if ( !is ) throw std::runtime_error( "stream dosent start with int" );

return nrv;
}

struct X
{
int i;
X( std::istream &is ) : i( istream_get( is ) ) {}
};

int main()
{
using namespace std;

istringstream iss( "12 A \n" );


X x( iss );

cerr << x.i << "\n";

try
{
X y( iss );
cerr << y.i << "\n";
}
catch ( std::runtime_error &e )
{
cerr << "exception: " << e.what() << "\n";
}
}

If you don't like the exception approach you could have istream_get
above return a /invalid/ value (-1) instread of throwing.

HTH.

Rob.
 
T

tarmat

That's great. thanks

tarmat wrote in
[snip]
class Derived : public Base
{
public:

Derived(std::ifstream& in);

}

The first variable of the stream is the identifying number, but I
don't know how to write the ctor of Derived to accomodate this. I want
to be able to do the equivalent of the following (which is obviously
not legal)

Derived::Derived(std::ifstream& in):Base(in >>)
{}

How do I get around this problem *without* changing the Base class?

#include <iostream>
#include <istream>
#include <sstream>
#include <stdexcept>

int istream_get( std::istream &is )
{
int nrv;
is >> nrv;

if ( !is ) throw std::runtime_error( "stream dosent start with int" );

return nrv;
}

struct X
{
int i;
X( std::istream &is ) : i( istream_get( is ) ) {}
};

int main()
{
using namespace std;

istringstream iss( "12 A \n" );


X x( iss );

cerr << x.i << "\n";

try
{
X y( iss );
cerr << y.i << "\n";
}
catch ( std::runtime_error &e )
{
cerr << "exception: " << e.what() << "\n";
}
}

If you don't like the exception approach you could have istream_get
above return a /invalid/ value (-1) instread of throwing.

HTH.

Rob.
 

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

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top