Using a function argument twice in a program

This is a discussion on Using a function argument twice in a program within the C++ Programming forums, part of the General Programming Boards category; Hi guys, The problem here is that the argument is a std::string and I was wanting to use it twice ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Using a function argument twice in a program

    Hi guys,

    The problem here is that the argument is a std::string and I was wanting to use it twice in the same concatenated return string. To illustrate:
    Code:
    string  img( string src, string href, string alt, string title = "", string extras = "")
    {
        return (string)"<a href=\"" += href += (string)"\"><img src=\"" += src
               += (string)"\" alt=\"" += alt += (string)"\" title=\"" +=
               /*( ( title.empty() )?alt:title ) +=*/ (string)"\" " += extras += "/></a>";
    }
    The function works fine as is, but uncommenting the ternary conditional (if that's even what they're called) screws it up. I'm guessing due to referencing issues. I'm not actually sure even how std::strings really work, but that's not going to be resolved any time soon. The basics are enough for me right now.

    I could obviously just copy the string onto another string and then use them both, but I was wondering if anyone knew of a way around this problem. Is there anything I can use?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can of course use an argument more than once. Whatever your error is, it is not because you use an argument multiple times.

    Why is that code so complicated? Use + instead of += if you are going to make it all on one line. Or spread it out over multiple lines to make it clearer, one += per statement. You also won't need the casts.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Try this:
    Code:
    string str = "dog";
    int num = 10;
    string returnString = "happy " + str + " sad " + ((num == 20)? "cat":str);
    cout<<returnString<<endl;
    Last edited by 7stud; 01-09-2006 at 09:19 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Hmm. I haven't been able to get it to work without the casts. Oops about the +=, but it's pretty simple really - at least it looks alright when it's in colour.

    I'll try it again. Cheers.

    Update:
    It works, and it looks far cleaner. Style is something I've yet to start on unfortunately. Thanks guys.
    Last edited by drrngrvy; 01-09-2006 at 08:24 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308

    Wink Wrong operator

    Actually, the first poster hinted at what is really the problem without realising it.
    You must not use += for what you are doing. You must in fact use just plain old +.

    That function isn't merely building up a string, it's actually modifying every string in the function (including some temporaries), which would become obvious if you declared the parameters as references (which you should do btw for efficiencies sake).

    To illustrate the problem using integers:
    int a=2, b=3, c=4, d=5;
    int result = a += b += c += d;
    Not only does result now equal 14 (as desired), but c is now 9, b is now 12, and a is now 14, as well.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Yeah I noticed that when I changed the '+=' to just '+' the funtion worked. I'm a newb and I'd just thought (for some misguided reason) that += was the concatenate operator for std::strings.

    As it happens, I've been trying to figure out the whole 'pass by reference' thing. I think I've just about got it, but I have a problem when passing string literals. Is there an easier/cleaner way to do it than type-casting them into std::strings in the calling arguement?

    Code:
    // For example, given a function:
    void do_stuff( string& arg )
    { cout<< arg; return; }
    
    // calling it like:
    do_stuff( "I think this is a string literal" );
    // doesn't work. But the only two ways I can see to do it are messy:
    string arg = "output this";
    do_stuff( arg );
    // OR
    do_stuff( (string)"output this" );

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You should take a const reference if you don't plan on changing the value. If you do that then you can just pass the literal since it will be automatically converted to a string.

    If you do need to modify the value, but don't want the caller's copy to be changed, you can use pass-by-value and like you did before. You won't need a cast there either because the conversion is automatic.

    If there are any cases where you do need to explicitly convert to a string, use the string constructor instead of a cast:
    Code:
    do_stuff( string("output this") );

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    I didn't even know you could cast literals like that! It's neater than the (string)"blahblah" way at least. Cheers Daved.

    One thing I've not been sure about since I started on c++ was whether concatenating an arguement onto another string and/or concatenating something onto that counts as 'mondifying' the arg?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    string1 = string2 + string3 only modifies string1.
    string1 += string2 only modifes string1.

    So in your original code, all five of your parameters should be const string& because you don't need to modify them. Either use + to concatenate them all in one line, or explicitly create a temporary string and use += to add each part one at a time to the temp string. Either way you aren't modifiying the parameters.

    BTW, to make an optional parameter for a const string&, use const string& title = string() which sets it to a default constructed (and therefore empty) string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21