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

stream constructors and derived classes

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
tarmat wrote in news:ug********************************@4ax.com:

[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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
That's great. thanks

On 10 Dec 2003 09:53:38 GMT, Rob Williscroft
<rt*@freenet.REMOVE.co.uk> wrote:
tarmat wrote in news:ug********************************@4ax.com:

[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.


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.