[HH] c++ strings?

Matthew Gillen me at mattgillen.net
Tue Nov 20 13:22:05 EST 2012


On 11/20/2012 01:13 PM, Jerry Feldman wrote:
> On 11/20/2012 01:02 PM, Matthew Gillen wrote:
>> On 11/20/2012 12:41 PM, Greg London wrote:
>>> In perl, it might look like this:
>>>
>>> sub upper_subroutine{
>>>     my ($string)=@_;
>>>     lower_subroutine("prefix".$string."postfix".timestamp());
>>> }
>>>
>>> Could someone give me an example of how to do this in C++ so that
>>> it looks as close to this perl code as possible?
>>
>> This will be useful to you for getting the timestamp aspect:
>>   http://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
>>
>> Other than that, use the '+' operator to concatenate C++ string
>> objects, much like you use the '.' operator in perl.
>>
>> Note that you can run into issues if neither of the operands to '+'
>> are actual std::string objects (e.g. "foo" + "bar" doesn't work in C++
>> like it would in perl, since the literal string is not automatically
>> promoted to std::string in some cases).
>>
>>> If this could happen without anyone having to die, that would
>>> be even better.
>>
>> That's asking a lot ;-)
>>
> std::string("foo") + std::string("bar") yields the std::string "foobar :-)

Right, because at least one of the operands is a std::string.  He'd 
actually be fine even using string literals in the places he indicated 
in his pseudo-code ^H^H^H perl code:
   String upper_subroutine(const std::string& s) {
     return lower_subroutine("prefix" + s + "postfix" + timestamp());
   }

That would work because 's' is a std::string for the first '+', etc.

Matt



More information about the Hardwarehacking mailing list