473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

stream constructors and derived classes

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
2 1609
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Beach Potato | last post by:
I guess I've been out of C++ for a while, since right now I don't seem to get a simple solution for overriding inherited constrictors. What worked in Borland C++ & Pascal (and Java, if I remember...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. ...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
3
by: zlf | last post by:
I am asked to complete a COM+ component, there is a class A derived from ServicedComponent. However, when executing , exception is thrown. Messaged: Unhandled Exception:...
4
by: nozyrev | last post by:
Hi, I apologize in advance if this is a very dumb question. I've been struggling with this problem for some time: I have an abstract base class called Base and n derived classes D1, D2, ....Dn. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.