[HH] c++ strings?

Jerry Feldman gaf at blu.org
Tue Nov 20 14:22:59 EST 2012


On 11/20/2012 01:22 PM, Matthew Gillen wrote:
> 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.
>
>
True. Also, for Greg your code does not need to create any classes, You
can use functions similar to the way you use them in Perl. You also
should use function declarations. But, other than that stuff is pretty
straightforward. Things like polymorphism don't come into play until you
use classes which is not really relevant here.

Also note that in Matt's example he used "const std::string&" This means
that s cannot be changed from within "upper_subroutine" and that he is
passing a reference to the string that was used to call
upper_subroutine. Without the &, it would have been a pass by value, and
the string would have been copied or passed by value. This is one area
where C programmers get confused. In C, strings are actually character
arrays, and when passed into functions, the pointer is used. But, in
C++, the string is a class. So the use of & tells C++ to pass by reference.

-- 
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id:3BC1EB90 
PGP Key fingerprint: 49E2 C52A FC5A A31F 8D66  C0AF 7CEA 30FC 3BC1 EB90


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 543 bytes
Desc: OpenPGP digital signature
URL: <http://lists.blu.org/pipermail/hardwarehacking/attachments/20121120/167d0678/attachment.sig>


More information about the Hardwarehacking mailing list