Do they have the same effect? - c++

This is a discussion on Do they have the same effect? - c++ ; I have code below: function decalre like below: void test_str(const std::string& str); //... 1 test_str(std::string("foo")); 2 { std::string tmp("foo"); test_str(tmp);} For 1 and 2 , do they have the same effect? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ ...

+ Reply to Thread
Results 1 to 4 of 4

Do they have the same effect?

  1. Default Do they have the same effect?

    I have code below:

    function decalre like below:
    void test_str(const std::string& str);
    //...
    1 test_str(std::string("foo"));
    2 { std::string tmp("foo"); test_str(tmp);}

    For 1 and 2 , do they have the same effect?


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  2. Default Re: Do they have the same effect?

    In article <1190076270.238204.201100@19g2000hsx.googlegroups.com>, zade
    <zhaohongchao@gmail.com> wrote:

    > I have code below:
    >
    > function decalre like below:
    > void test_str(const std::string& str);
    > //...
    > 1 test_str(std::string("foo"));
    > 2 { std::string tmp("foo"); test_str(tmp);}
    >
    > For 1 and 2 , do they have the same effect?

    as long as you DON'T have also void test_str(const char *);
    yes std::string::string(const char *) will do an implicit construction
    of a temporary string and pass a const reference to it to test_str.
    The effect is the same and the code should be nearly identical.

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  3. Default Re: Do they have the same effect?

    "zade" <zhaohongchao@gmail.com> wrote in message
    news:1190076270.238204.201100@19g2000hsx.googlegroups.com...
    >I have code below:
    >
    > function decalre like below:
    > void test_str(const std::string& str);
    > //...
    > 1 test_str(std::string("foo"));
    > 2 { std::string tmp("foo"); test_str(tmp);}
    >
    > For 1 and 2 , do they have the same effect?


    Add 3.
    3 test_str( "foo" );

    The same effect in that that function test_str receives a reference to a
    std::string. 1 and 3 are very similar. Both create a temp std::string and
    pass a reference to it. However, without the const keyword, they would not
    be the same. You can not pass a temporary variable to a non constant
    reference.



    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  4. Default Re: Do they have the same effect?

    On Sep 18, 3:29 pm, Carl Barron <cbarron...@adelphia.net> wrote:
    > In article <1190076270.238204.201...@19g2000hsx.googlegroups.com>, zade
    >
    > <zhaohongc...@gmail.com> wrote:
    > > I have code below:

    >
    > > function decalre like below:
    > > void test_str(const std::string& str);
    > > //...
    > > 1 test_str(std::string("foo"));
    > > 2 { std::string tmp("foo"); test_str(tmp);}

    >
    > > For 1 and 2 , do they have the same effect?

    >
    > as long as you DON'T have also void test_str(const char *);
    > yes std::string::string(const char *) will do an implicit construction
    > of a temporary string and pass a const reference to it to test_str.
    > The effect is the same and the code should be nearly identical.


    Look again at his code. He /explicitly/ creates a temporary
    std::string in case #1. Even if there is a test_str(const char *), it
    won't be considered. I can't see /any/ difference between the two
    cases. In both cases, a std:string is constructed, test_str is passed
    a const reference to that string, and then it is destructed.

    If there were multiple arguments, then the explicit named variables
    inside a block would allow the author to control the order of
    construction and destruction (if that was important).

    To the OP: why the question?


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


+ Reply to Thread

Similar Threads

  1. INDS2 Need feathered effect only on one side(kind of "Rocket trail" effect)
    By Application Development in forum Adobe Indesign
    Replies: 5
    Last Post: 09-16-2007, 06:43 AM
  2. the NaN effect :-|
    By Application Development in forum Idl-pvwave
    Replies: 9
    Last Post: 06-13-2007, 03:51 PM
  3. Need Effect - Handwriting text animation effect
    By Application Development in forum Adobe After Effects
    Replies: 1
    Last Post: 10-25-2006, 10:33 AM
  4. How to get this effect?
    By Application Development in forum Graphics
    Replies: 7
    Last Post: 08-30-2006, 05:12 PM
  5. Set effect of ^K?!
    By Application Development in forum Pine
    Replies: 2
    Last Post: 05-15-2006, 06:53 PM