473,700 Members | 2,569 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 3726
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
2680
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
14406
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 ostream_iterator, but there doesn't seem to be an analogous way to handle it using istream_iterator. ...
0
1085
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
4136
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
3729
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 the characters in the stream. There is a way to get the streambuf object associated with the...
13
10985
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 with gcc!). When reading from a named pipe on gcc, it returns immediately - no errors, just...
3
3378
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 document. When I first tested these functions everything seemed to work fine (and basically, it does),...
4
3710
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 class objects and sets 0s to POD types. This is what I've learned from the books (especially...
2
4434
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) { std::cout << "real: ";
0
8648
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9081
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8929
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7816
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6564
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2031
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.