What is a better style?

This is a discussion on What is a better style? within the C++ Programming forums, part of the General Programming Boards category; I have some functions which perform string transformations. Since they use the original string, but return a copy of the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    What is a better style?

    I have some functions which perform string transformations. Since they use the original string, but return a copy of the modified string, what is better style?
    Code:
    string transform1(const string& s) { //uses const reference
    	string s2 = s; //make a copy
    	//do some stuff on the copy
    	return s2;
    }
    
    string transform2(string s) { //passed by value
    	//do some stuff on the copy already passed in
    	return s;
    }
    Last edited by jw232; 09-14-2008 at 10:50 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    I'd say that in this case, it really is a matter of style, and both are fine. The former has the advantage of being more consistent since strings are normally passed by (const) reference; the latter has the advantage of being a little less verbose.

    Note that the const for the latter is wrong. You actually want to change s, so s should be non-const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    I see. Thanks for the correction. Is there any performance difference?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Is there any performance difference?
    The MinGW port of g++ 3.4.5 does produce different assembly code for the two cases, but I am afraid that I have neither the time nor skill at the moment to determine if this makes for any real difference in efficiency.

    That said, I daresay that there is no real difference: the time will likely be dominated by the copying of characters, which is the same in both cases. If you are particularly concerned, profile your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If the string you pass is not already a variable, the latter is faster.
    Code:
    string s = transform1("Hello, World!");
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  3. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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