Thread: string reference

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    13

    string reference

    Hi,
    I have the following function:
    Code:
    string &find (string &name);
    It returns a concatenated string of name and another string. But if the specified name doesn't exist, the function has to indicate this. I could just figure out one solution: throwing an exception. But I think this is not very useful in this case. Can you imagine another solution (like returning iterator::end (), but for strings).

    Thanks
    matott

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This protptype won't work very well.
    You're trying to return a reference to what ? ( shouldn't be a local variable )
    I'd try something like this
    Code:
    string find( string & name );
    and return an empty string in case of name not found.
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Oh yes, its wrong. Else it would be some kind of dangling reference.

    I thought about the empty string solution, but can you imagine another solution - maybe by changing the function prototype?

    matott

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I thought about the empty string solution, but can you imagine another solution - maybe by changing the function prototype?
    It depends on what this function is used for. Do you have any usage examples?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Quote Originally Posted by laserlight View Post
    It depends on what this function is used for. Do you have any usage examples?
    It generates a list from directory entries, searches for the name in this list and returns the name and a version string which is generated from the filename.
    Code:
    name#version

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, then what happens if the name is not in the list? I agree, having an empty string does not help, unless you use "empty string" as an error condition to mean "file not found". Throwing an exception would also be suitable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    13
    Ah, then what happens if the name is not in the list?
    The programme reports to the user that it's not an the list.

    I agree, having an empty string does not help, unless you use "empty string" as an error condition to mean "file not found".
    It would be something like NULL in this case, but avoiding this solutions seems to be quite difficult.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could return a bool and have the user pass in a string to be modified. You could also return a pair<string, bool> that holds the string value and a true/false for whether it succeeded. I didn't look at your problem closely enough to see whether I think either of those options are actually more appropriate.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    being a classical example of a source function, what would be wrong with
    Code:
    auto_ptr<string> find(const string& name)
    ?

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    auto_ptr's are not copyable, so you can't return them like that. A Boost smart pointer would work though.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In fact, they are copyable, and this is a perfect example of how and when you would want to copy them.

    The auto_ptr copy semantics are different than normal copy semantics in that they transfer ownership, thereby modifying the source of the target. In this case, that's what you want since the source of the copy will be a local auto_ptr inside the function. After the copy takes place, the local auto_ptr will be destroyed but that won't affect the internal pointer because it will no longer have ownership. The target of the copy will own the pointer, and it can do whatever it likes with the string and when it goes out of scope the internal pointer and the string will be properly destroyed (assuming no other ownership transfer has occurred in the meantime).

    I think this might also be an example of when C++0x's move semantics will make the situation easier and it won't require dynamic memory.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well I'll be...
    I just re-read the auto_ptr references in "Effective STL" & "C++ Coding Standards" and looked at the auto_ptr source code, and it looks like you're right. When the books said you can't store auto_ptr's in STL containers because of their copy semantics, I thought that meant all copying was out of the question.
    I'd still feel safer using a reference counted smart pointer though.

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by cpjust View Post
    I'd still feel safer using a reference counted smart pointer though.
    I do not think thats safer. because not so clear what the function is supposed to do like while using auto_ptr.
    auto_ptr has 4 main functions in c++ i know of:
    -exception safety
    -source functions (like yours)
    -sink functions
    -large automatic variables

    you can trust it. it's designed exactly for your actual use. while returning a auto_ptr every programmer knows: "that function allocates memory on the heap and returns a handle to it." shared_ptr can mean the same....or not
    Last edited by pheres; 08-20-2007 at 12:12 AM.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    while returning a auto_ptr every programmer knows: "that function allocates memory on the heap and returns a handle to it."
    Well nobody can really know that for sure, they just assume it. But the developer who wrote the function might not understand all the implications of using an auto_ptr and might save the auto_ptr in their class before returning it and thereby invalidating their own copy... I've seen some developers that I used to work with do some scary things with auto_ptr's (like store char* arrays in them).
    Also, what if the function starts out written correctly & returning an auto_ptr, but later as the code evolves, someone decides that they want to keep a local copy of the pointer before returning it. The proper thing to do would be to switch to smart_ptr's, but then that would change the function signature and require all calling code to be changed also...

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Using auto_ptr is "unsafe" in that situation only if users are unfamiliar with it. That is probably pretty common, though, so the point is certainly something to consider. If you're not familiar with auto_ptr, it would be better to use one of the other mentioned solutions while you learn and gain a bit of experience with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reference string
    By chocolatecake in forum C Programming
    Replies: 6
    Last Post: 05-14-2009, 06:02 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM