Archived

This topic is now archived and is closed to further replies.

randomZ

Implementation of std::string::c_str()?

Recommended Posts

Just a quick question about STL: std::string implements the methods data() and c_str(). "The C++ Programming Language" says that the arrays they return remain in the possession of the string, and the user shouldn''t try to free them. Both of these methods can also be called on const strings. My question: How does the string keep track of the arrays it created for deleting them later? It can''t be in a member variable, as that couldn''t be changed in a const method... Background: I''m trying to implement a string class that can convert from and to all Unicode formats. --- Just trying to be helpful. Sebastian Beschke Just some student from Germany http://mitglied.lycos.de/xplosiff

Share this post


Link to post
Share on other sites
you could of course look at the header and read for yourself. anyway... of course the string has a member that represents the string

Share this post


Link to post
Share on other sites
OK, it seems it does. I was confused by the book saying that the functions *copied* the string data.

This doesn''t solve my problem, because for unicode conversions, I must create a copy of the string. So the solution would be to

- either don''t allow conversion operations on const strings (bad)

- or require the user to delete the string himself

- or perhaps use a smart pointer (probably overkill).

Does anyone have any tips about this?


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When you convert to unicode, you could give out a unicodestringobject that keeps track of the data and has a c_str() method itself giving a pointer to the data ?

Share this post


Link to post
Share on other sites
OK, based on what you said, I think I''ll be doing this:

- have thin wrapper classes around arrays of various char types, which manage memory allocation and so on; could be a template
- have one universal string class, which can convert its internal string and return instances of these wrapper classes

Sounds quite OK to me, what do you think?


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff

Share this post


Link to post
Share on other sites
Oooohhh...

I completely forgot that keyword existed. You''re right, it''s exactly what I asked for.

Dunno if I''ll use the wrapper classes anyway - they have the advantage of having a different life time from the original string, thus being owned by the user - plus I''ve already started writing one... I have to think about it.

Thanks everyone!


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff

Share this post


Link to post
Share on other sites
Actually I think some implementations hand out the private char array pointer and keep it null terminated the entire time. I don''t think its required that the string make a copy of the data.

I would just aggregate a std::wstring in your UnicodeString class and then add the necessary operators.

Share this post


Link to post
Share on other sites
Yep; the g++ implementation does that, as I could see in the header file. I could use std::wstring, but I thought writing the stuff on my own couldn''t hurt (from an educational point of view ). Also, I somehow like to write stuff myself, even if it''s totally impractical.


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff

Share this post


Link to post
Share on other sites