-
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.
-
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)
-
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.