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

Started by
7 comments, last by randomZ 20 years, 1 month ago
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
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Advertisement
you could of course look at the header and read for yourself. anyway... of course the string has a member that represents the string
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
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
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 ?
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
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Maybe I misunderstood it''s purpose, but isn''t this a job for the mutable keyword? Mutable members can be modified from a const function.
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
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/

This topic is closed to new replies.

Advertisement