Talk:C++ Programming/Code/IO
Add topicremark from a newbe
[edit source]for 10/4/2007,from ("safe bool" idiom) and down i don't understand what's going on.--87.65.223.208 19:33, 10 April 2007 (UTC)
- Have you taken a look at the examples ? Do you understand the basic reason or nothing at all? If you can ask a direct question so we can improve the text use the Q&A page, the response will then be included on the text. --Panic 23:51, 10 April 2007 (UTC)
Member vs. global
[edit source]Why is this a member function:
ostream& operator<< (int& val );
But is this a global function:
ostream& operator<< (ostream& out, char c );
Why? 82.139.85.56 06:48, 19 September 2007 (UTC)
- I don't completely understand what you are asking. Do you understand classes and the use of friend on the example source ? --Panic 20:26, 19 September 2007 (UTC)
I do understand basic C++ classes, templates, etc. I was wondering why << is a member for int while it is a global function for char. Do you understand what I'm asking now? 82.139.85.236 14:56, 20 September 2007 (UTC)
I think I do... I've not examined the standard definition but logic dictates that if the global operator<< can been overloaded the scope and how the overloading is performed can and should be optimized. You should not add friends to existing classes unless you are responsible for their maintenance. You must also keep in mind that std::ostream is at the core a typedef alias, and remember that at least one operand of any overloaded operator must be of some user-defined type. This is why operator<< and operator>> are almost always implemented as non-member functions, exception if any are due to optimization or added functionality. As an example of this logic I call your attention to:
// Input and output ostream& operator<<(ostream& os, const string& s); istream& operator>>(istream& is, string& s); istream& getline(istream& is, string& s, char delimiter = '\n');
This String-related global operators and functions are defined in the <string> header. As you can see this is done so mostly for "logistic"/organizational reasons that any particular optimization (besides that member can access inside a particular scope)... At least this is what I think about the subject. --Panic 19:05, 20 September 2007 (UTC)
Number rounding example
[edit source]This "short" example takes up half the page and, as far as I can tell, doesn't logically follow from the preceding text. I propose it be moved to a more suitable page (by a editor more experienced than I). - Alexander Swift 140.254.254.241 (discuss) 22:56, 1 March 2013 (UTC)