473,585 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istream in init list

Hi,

In some threads, some people mentioned that variable initialization is
best performed in an initialization list.

Is there a way to initialize a variable from an istream in an
initialization list?
Example:
class My_Class
{
public:
My_Class(istrea m& inp);
private:
unsigned int value;
};

My_Class :: My_Class(istrea m& inp)
/* [1] */ : value(/* ??? */)
{ ; }
I looked at the istream class and there is no method for
inputting an integer. The extraction operator is defined as a
non-member function.

So, if it is possible to initialize from an istream in the
initialization list, what is replaces "/* ??? */" in [1] above?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 19 '05 #1
4 3723
Thomas Matthews escribió:
Is there a way to initialize a variable from an istream in an
initialization list?
Example:
class My_Class
{
public:
My_Class(istrea m& inp);
private:
unsigned int value;
};

My_Class :: My_Class(istrea m& inp)
/* [1] */ : value(/* ??? */)
{ ; }
I looked at the istream class and there is no method for
inputting an integer. The extraction operator is defined as a
non-member function.

So, if it is possible to initialize from an istream in the
initialization list, what is replaces "/* ??? */" in [1] above?


The simplest way is define a constructor for the class of "value" that
takes an & istream as parameter and simply doing:
value (inp)

If not desired, simply write a function that takes & istream as
parameter and return a value and do:
value (myfunction (inp) )

myfunction can be an static member of My_Class, if needed or desired.

In your concrete case, where value is an unsigned int and not a member
of a class, only the second option is possible.

Regards.
Jul 19 '05 #2
Thomas Matthews wrote:
Hi,

In some threads, some people mentioned that variable initialization is
best performed in an initialization list.

Is there a way to initialize a variable from an istream in an
initialization list?
Example:
class My_Class
{
public:
My_Class(istrea m& inp);
private:
unsigned int value;
};

My_Class :: My_Class(istrea m& inp)
/* [1] */ : value(/* ??? */)
{ ; }
I looked at the istream class and there is no method for
inputting an integer. The extraction operator is defined as a
non-member function.

So, if it is possible to initialize from an istream in the
initialization list, what is replaces "/* ??? */" in [1] above?


int ReadInt(istream &is)
{
int i;
is >> i;
return i;
}

My_Class::My_Cl ass(istream &inp) : value(ReadInt(i np)) {}

Short of something like this, I don't see a way. This is of questionable
value also, because there's no error checking. You could put error
checking in ReadInt(), but the only thing it could really do to signal
the error is throw an exception.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
Kevin Goodsell wrote:
Thomas Matthews wrote:
Hi,

In some threads, some people mentioned that variable initialization is
best performed in an initialization list.

Is there a way to initialize a variable from an istream in an
initialization list?
Example:
class My_Class
{
public:
My_Class(istrea m& inp);
private:
unsigned int value;
};

My_Class :: My_Class(istrea m& inp)
/* [1] */ : value(/* ??? */)
{ ; }
I looked at the istream class and there is no method for
inputting an integer. The extraction operator is defined as a
non-member function.

So, if it is possible to initialize from an istream in the
initialization list, what is replaces "/* ??? */" in [1] above?


int ReadInt(istream &is)
{
int i;
is >> i;
return i;
}

My_Class::My_Cl ass(istream &inp) : value(ReadInt(i np)) {}

Short of something like this, I don't see a way. This is of questionable
value also, because there's no error checking. You could put error
checking in ReadInt(), but the only thing it could really do to signal
the error is throw an exception.

-Kevin


Your suggestion appears to be less efficient than reading in the
value in the constructor:
/* [2] */
My_Class :: My_Class(istrea m& inp)
{
inp >> value;
}

IMHO, this is a case where assignment in the body of a constructor
is more efficient than in the initializer list.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 19 '05 #4
Thomas Matthews wrote:
Kevin Goodsell wrote:

int ReadInt(istream &is)
{
int i;
is >> i;
return i;
}

My_Class::My_Cl ass(istream &inp) : value(ReadInt(i np)) {}

Short of something like this, I don't see a way. This is of
questionable value also, because there's no error checking. You could
put error checking in ReadInt(), but the only thing it could really do
to signal the error is throw an exception.

-Kevin

Your suggestion appears to be less efficient than reading in the
value in the constructor:
/* [2] */
My_Class :: My_Class(istrea m& inp)
{
inp >> value;
}

IMHO, this is a case where assignment in the body of a constructor
is more efficient than in the initializer list.


I imagine the compiler could generate the same code for both (assuming
it has access to the definition of ReadInt in order to inline it). But
generally it would probably be (slightly) less efficient. Anyway, I
don't think I'd use the separate function method unless there was a very
good reason to want to use the initializer list (e.g., if 'value' were
const).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2677
by: Juan Carlos CORUÑA | last post by:
Hello all, I have developed an automation server using Mark's win32 extensions and I must return a IStream COM object from one method of the automation server. Here is an extract of my code: class Stream: _public_methods_ =
8
14399
by: Dave Moore | last post by:
Is there any way to specify an istream separator sequence? For example, suppose I have a record consisting of a list of comma-separated values (no whitespace). I want to set the istream up so that formatted input operations using operator>> will recognize the comma as a field separator. I know how to do this for an output stream using an...
0
1082
by: ClockWinder | last post by:
I am defining a COM IDispatch interface. The following causes a compiler error: HRESULT Meth1( IStream* data); HRESULT Meth2( SAFEARRAY** list); error C2440: 'initializing' : cannot convert from 'VARIANT' to 'IStream *' Whereas removing the satype attribute or switching the id values does not.
0
4127
by: prakash | last post by:
Dear Friends I am new guy to Visual C++.NET I've program to save website as a image vc++.net . It have a function "SaveSnapshot" to save the webpage as an image On that function ifor saving as a image it uses image.Save method() This image.Save function have two overloaded form's first one is file name
13
3722
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of these methods EXTRACT the data at one point or another. The other problem is there appears to be NO WAY to get at the actual buffer pointer (char*) of...
13
10974
by: Gianni Mariani | last post by:
What I would like to do is read bytes from a stream, any number and any time. I would like it to wait until there are any bytes to read. I want the exact same functionality as cstdio's "fread" but in a std::istream. It appeared at first that "readsome" would do exactly what I wanted but it appears not to work very well at all (at least...
3
3373
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound file aren't really useful to a user, I use the IStream::Read function together with a StreamWriter to extract single files from my compound...
4
3698
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for...
2
4421
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream & operator>(std::istream & in, const Complex & a); // the methods correspond to the friend std::istream & operator>(std::istream & in, const Complex & a) {...
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.