Thread: A couple of questions regarding classes and strings.

  1. #16
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    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.
    Last edited by okinrus; 06-12-2004 at 10:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some questions with strings, vectors, references...
    By samus250 in forum C++ Programming
    Replies: 11
    Last Post: 07-10-2008, 02:31 PM
  2. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 01:04 AM
  3. Need help with using strings in classes
    By orikon in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2005, 03:01 PM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Help with classes & strings members
    By GrNxxDaY in forum C++ Programming
    Replies: 7
    Last Post: 07-21-2002, 08:29 PM