A couple of questions regarding classes and strings.

This is a discussion on A couple of questions regarding classes and strings. within the C++ Programming forums, part of the General Programming Boards category; So can't this be extended to every other function regardless of the parameter type it requires or returns (int, char, ...

So can't this be extended to every other function regardless of the parameter type it requires or returns (int, char, string, struct, class, etc...). It seems that this may be beneficial when passing and returning large objects instead of making a copy of them, so why are we applying it to strings if they usually don't take up a lot of space?

You shouldn't pass builtin types such as integers by constant reference because the copying involved is neligable. Even passing small structs such as a point struct won't really be a problem. Also, you generally pass references and pointers constant when your function is not changing the data the reference or pointer references. It's not so much the space the object itself has but how much time the copy constructor of the object takes. std::string could only consist of a pointer to the null-terminated string but this null-terminated string would have to be allocated memory for and copied inside of std::string's copy constructor.

While you will most likely need to time your program to see how much slower it is, it's probably best to get into the habit of returning and passing by constant reference for these objects. Depending on what you class is doing, this could be a problem. It shouldn't matter here because the user input of reading the string is so much slower than the copying and memory allocation.