Thread: Curiosity question...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Curiosity question...

    I really just started getting into programming again, as a major hobby and have been curious of a few things most about function arguments.
    if I have the following
    Code:
    bool CreateNewString(std::string);
    bool CreateNewInt(int);
    bool CreateNewChar(char);
    bool CreateNewFloat(float);
    from what I understand those prototypes will make copies of the arguments, but if I do this
    Code:
    bool CreateNewString(const std::string&);
    bool CreateNewInt(const int&);
    bool CreateNewChar(const char&);
    bool CreateNewFloat(const float&);
    These will not create copies every time I run the function correct? so therefor using this along with inline should increase the speed of frequently used functions?
    I understand that this is a relatively stupid thing to ask but curiosity kills. Thank you for any assistance.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    bool CreateNewInt(const int&);
    bool CreateNewChar(const char&);
    bool CreateNewFloat(const float&);
    For primitives it makes no difference (and pointer types too. I dunno if pointers are primitives)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Using reference arguements instead of copies makes a difference, with objects, because it saves a call to the copy constructor of that object, and the actually coping required (which applies even to the defult copy constructor). Types passed by reference are actually passed by pointer under the hood, so instead of creating a multi byte object on the stack, a 4 byte pointer is created. For primitives this is not an optimization; primitives are small in memmory, and copying them over has less overhead than dereferencing a pointer.

    The speed gain from useing references has nothing to do with inline functions. Barring optimisations, the gain from using references is uneffected by weither the function is inline.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM