[HH] c++ strings?

Jerry Feldman gaf at blu.org
Tue Nov 20 16:34:31 EST 2012


Not necessarily. What you are saying by using:
void Note(const string msg&);
is that Note does not side effect msg. But, in your case you are passing
in a string literal. But if you were using an actual string variable,
you could pass by reference without the const, and make changes to it.
Generally, in a small program that does not live long, it does not
matter too much. But, what if you were writjng a run-time library
function that gets called frequently, then it would matter. In general
it is not good practice to pass by reference and modify the variable
passed in, but sometimes it is a desired effect.

Let's say you have a class that contains a variable. Normally you would
hide that variable from anyone out of your class, but you would provide
accessors and mutators. You use the accessor functions to get the value
of a variable, and use the mutator functions to change the values. But,
I think we are getting off topic here. I think you have a program that
should work well

On 11/20/2012 04:15 PM, Greg London wrote:
> So, if I have a subroutine that passes by reference,
> and I want to pass in something that can't be modified,
> I have to put it into some intermediate variable first?
>
> That seems.... inefficient.
>
>
>
>> On 11/20/2012 03:57 PM, Matthew Gillen wrote:
>>> On 11/20/2012 03:41 PM, Greg London wrote:
>>>> Why would I need "const" when I pass by reference, but not need const
>>>> when
>>>> I pass by value?
>>> Because references are not pointers, and you cannot "re-seat" the
>>> reference.
>>>    http://www.parashift.com/c++-faq/reseating-refs.html
>>>
>>> If you want the nitty-gritty details on how references are implemented:
>>>   http://www.parashift.com/c++-faq/overview-refs.html
>> Sorry, I jumped the gun.  Your problem had nothing to do with re-seating
>> a reference.
>>
>> The problem in your example:
>>   ...
>>   Note("hello world\n");
>>   ...
>>   void Note( string &msg){ ...
>>
>> is that "hello world", by virtue of being a string literal, *is* a const
>> string, but your function prototype (when sans-const) is indicating that
>> the function reserves the right to modify the string.
>>
>> When you pass by value, you're always getting a copy, so the effect is
>> essentially:
>>   Note(new std::string("hello world\n");
>>
>> ...and there's never a problem modifying your local copy of the original
>> string. However, if you pass by reference, you're not making a new copy,
>> so if the source object has some restrictions (e.g. is const), then you
>> can't pass it to functions that reserve the right to modify it.
>>
>> Const-correctness can be tricky when you're first learning it, but if
>> you get it right it is a great tool to make the the compiler help you.
>>
>> Matt
>> _______________________________________________
>> Hardwarehacking mailing list
>> Hardwarehacking at blu.org
>> http://lists.blu.org/mailman/listinfo/hardwarehacking
>>
>


-- 
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/5690d48e/attachment.sig>


More information about the Hardwarehacking mailing list