[HH] c++ strings?
Jerry Feldman
gaf at blu.org
Tue Nov 20 14:35:24 EST 2012
On 11/20/2012 01:57 PM, Greg London wrote:
>
> Ah, that put me in the right direction.
>
> main.cpp looks like this:
> #################################
> #include <iostream>
> using namespace std;
>
> #include "logger.h"
>
> int main ()
> {
> Note("hello world\n");
> return 0;
> }
>
>
> logger.h looks like this:
> ##################################
> #include <iostream>
> using namespace std;
>
> extern void Note(string msg);
>
>
> logger.cpp looks like this:
> ##################################
> #include <iostream>
> using namespace std;
>
> void _say(string msg){
> cout<<msg;
> }
>
> void Note(string msg){
> _say("Note: "+msg);
> }
>
>
>
>
>
> my compile command looks like this:
> #######################################
> g++ logger.cpp main.cpp
>
>
>
>
> This looks like quite lovely code now.
>
> Thanks!
>
Almost perfect. I would make a few minor changes.
void _say(const string msg&){
cout<<msg;
}
void Note(const string msg&){
_say("Note: "+msg);
}
Minor efficiencies. In your code in when note() is called the string is copied so msg is a copy of the input. In _say("Note: "+msg);
you are creating a new string, but that string is also copied. Setting up a call by reference saves 2 copies in this case. The compiler can optimize the _say function to where the call by reference does not matter, but not the note() function which is external.
--
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/83ff2a5d/attachment.sig>
More information about the Hardwarehacking
mailing list