home bbs files messages ]

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,155 of 33,346   
   Ulrich Eckhardt to All   
   Re: why can't i access std::string with    
   17 Apr 12 13:37:48   
   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 17.04.2012 00:09, schrieb news.eternal-september.org:   
   > I'd like to access C++ strings the way i did in C   
      
   C strings are still available in C++ if you need that, but I don't think   
   you do.   
      
      
   >  string foo="foo";   
   >  string bar="bar";   
   >   
   >  foo[1] + bar[2];   
      
   The problem is this expression. "foo[1]" yields a char, "bar[2]", too.   
   Adding those two does not create a string, it just treats those two   
   chars as integers. This is different from e.g. Python, where these   
   expressions yield strings of size 1, but it's the same as in C.   
      
   What you can do is use one of the substr() member function overloads.   
   Another way is that you avoid the + operation on chars, and use two +=   
   operations on the string instead. You could also explicitly convert (at   
   least) one of the two chars to a string, which would then trigger a   
   different operator+ overload.   
      
      
   Good luck!   
      
   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