Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,852 of 33,346    |
|    Ulrich Eckhardt to All    |
|    Re: A beginner's string/ostream question    |
|    06 Feb 13 08:09:43    |
   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 06.02.2013 08:09, schrieb James Moe:   
   > string msg1, msg2;   
   > std::ostringstream stro(msg1);   
      
   Wait: Why are you passing the empty string 'msg1' to the ostringstream's   
   constructor? Just to make that clear, it will not cause the output   
   written to 'stro' to end up in 'msg1'! In order to get at the written   
   text, call the 'str()' function. Your code then looks like this:   
      
    ostringstream s;   
    s << foo << bar;   
    string msg = s.str();   
      
   I guess this already explains and fixes that your "string usually ends   
   up empty".   
      
      
   > msg2 = string(" messages in [") +   
   > string(foldname) +   
   > string("]");   
      
   If 'foldname' was a string, you could just do   
      
    msg2 = "...[" + foldname + "]";   
      
   However, since this is the last that gets written to the ostream in both   
   cases below, you could directly write this to the ostream.   
      
   Also, a short style suggestion: Don't declare all variables on top of   
   the function. This only used to be necessary in ancient C code, but not   
   any longer in C++ or current C. Instead, declare and initialize them   
   right at the place they are used. Preferably, sprinkle a 'const' in   
   there, too, in case you don't want to change this later on:   
      
    string const msg2 = "...[" + foldname + "]";   
      
   This makes it clear what this string contains, it reduces its scope and   
   makes the compiler check that you don't accidentally modify it.   
      
      
   HTH   
      
   Uli   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca