Return-Path: Delivered-To: apmail-stdcxx-commits-archive@www.apache.org Received: (qmail 53484 invoked from network); 15 Nov 2008 17:53:58 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 15 Nov 2008 17:53:58 -0000 Received: (qmail 84922 invoked by uid 500); 15 Nov 2008 17:54:06 -0000 Delivered-To: apmail-stdcxx-commits-archive@stdcxx.apache.org Received: (qmail 84896 invoked by uid 500); 15 Nov 2008 17:54:06 -0000 Mailing-List: contact commits-help@stdcxx.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@stdcxx.apache.org Delivered-To: mailing list commits@stdcxx.apache.org Received: (qmail 84887 invoked by uid 99); 15 Nov 2008 17:54:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 15 Nov 2008 09:54:06 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 15 Nov 2008 17:52:53 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C7F7C238898F; Sat, 15 Nov 2008 09:53:07 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r717894 - /stdcxx/trunk/doc/stdlibug/34-2.html Date: Sat, 15 Nov 2008 17:53:07 -0000 To: commits@stdcxx.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081115175307.C7F7C238898F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sebor Date: Sat Nov 15 09:53:07 2008 New Revision: 717894 URL: http://svn.apache.org/viewvc?rev=717894&view=rev Log: 2008-11-15 Martin Sebor STDCXX-1024 * doc/stdlibug/34-2.html (34.2.3): Removed ill-formed code example. Modified: stdcxx/trunk/doc/stdlibug/34-2.html Modified: stdcxx/trunk/doc/stdlibug/34-2.html URL: http://svn.apache.org/viewvc/stdcxx/trunk/doc/stdlibug/34-2.html?rev=717894&r1=717893&r2=717894&view=diff ============================================================================== --- stdcxx/trunk/doc/stdlibug/34-2.html (original) +++ stdcxx/trunk/doc/stdlibug/34-2.html Sat Nov 15 09:53:07 2008 @@ -158,7 +158,7 @@

The effect is the same as in the previous solution, because the standard output stream std::cout is connected to the C standard file stdout. This is the simplest of all solutions, because it doesn't involve reassigning or sharing stream buffers. The output file stream's buffer is simply connected to the right file. However, this is a nonstandard and nonportable solution.

34.2.3 Using Pointers or References to Streams

-

If you do not want to deal with stream buffers at all, you can also use pointers or references to streams instead. Here is an example:

+

If you do not want to deal with stream buffers at all, you can also use pointers to streams instead. Here is an example:

     int main(int argc, char *argv[])
    @@ -170,7 +170,7 @@
          fp = &std::cout                                          //3
     
       *fp << "Hello world!" << std::endl;                         //4
    -  if (fp!=&std::cout) 
    +  if (fp != &std::cout)
          delete fp;
     }
     
@@ -181,25 +181,7 @@ //3Otherwise, a pointer to std::cout is used. //4Output is written through the pointer to either std::cout or the named output file. - -

An alternative approach could use a reference instead of a pointer:

- -
    -int main(int argc, char *argv[])
    -{
    -  std::ostream& fr;
    -  if (argc > 1)
    -    fr = *(new std::ofstream(argv[1]));
    -  else
    -    fr = std::cout;
    -
    -  fr << "Hello world!" << std::endl;
    -
    -  if (&fr!=&std::cout) 
    -    delete(&fr);
    -}
    -
-

Working with pointers and references has a drawback: you must create an output file stream object on the heap and, in principle, you must worry about deleting the object again, which might lead you into other dire straits.

+

Working with pointers has a drawback: you must create an output file stream object on the heap and, in principle, you must worry about deleting the object again, which might lead you into other dire straits.

In summary, creating a copy of a stream is not trivial and should only be done if you really need a copy of a stream object. In many cases, it is more appropriate to use references or pointers to stream objects instead, or to share a stream buffer between two streams.