🔒 Locked
string classes??
Advertisement
Original Post
Yes, you could write your own String class. String classes more or less replace the use of char* you had to use in C if you wanted to deal with strings.
The reason is that a ''real'' string class is usually a lot easier to use then char*. No need to worry about memory allocation and such. (Unless you''re writing the string class yourself, then you would need to worry about it inside that class)
But since there is a few string classes out there you could use, unless you really want to write one of your own, I wouldn''t really worry about doing it.
The reason is that a ''real'' string class is usually a lot easier to use then char*. No need to worry about memory allocation and such. (Unless you''re writing the string class yourself, then you would need to worry about it inside that class)
But since there is a few string classes out there you could use, unless you really want to write one of your own, I wouldn''t really worry about doing it.
quote:
Original post by julienX
Is it necessary to write your own string classes, if yes then what for?
No. The C++ library has a standard std::string class that is good enough for most uses.
Some people prefer to implement their own in quest of some extra performance - whether they're right or not is subject to much discussion and is irrelevant here.
That being said, as a learning experience, it's as good as anything else. Just don't become so attached to your class that you would overlook what the C++ library offers.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on December 14, 2002 9:41:30 AM]
If you are a new programmer, I heavily recommend against using string classes. Learn how real strings (null-terminated character arrays) work, and how to use them effectively. Once you have a solid knowledge of real strings, then you can start using string classes where necessary.
- Andy Oxfeld
- Andy Oxfeld
Could you define "real strings"?
A string class is a ''real string''... char* is sort of a workaround, because C did not offer a variable of type string. (Unlike Pascal, or other non OO languages)
And, if he''s trying to learn OO I think using std::string is the OO way of working with strings.
I''m not trying to say learning how char* works is wrong, it''s definitly usefull, but I think using std::string is easier and less likely to raise an error then char*, especially if you''re new to programming.
A string class is a ''real string''... char* is sort of a workaround, because C did not offer a variable of type string. (Unlike Pascal, or other non OO languages)
And, if he''s trying to learn OO I think using std::string is the OO way of working with strings.
I''m not trying to say learning how char* works is wrong, it''s definitly usefull, but I think using std::string is easier and less likely to raise an error then char*, especially if you''re new to programming.
I''d rather not start another argument about this, but...
A string class is a wrapper for a C string (or "real string" as I called it). I don''t see how a string class can be considered a real string when it effectively does the same thing you would be doing, and just hides this from you.
No matter how you look at it, string is still not an intrinsic type in C++, and should never be. You don''t need any include files for int, float, etc., but you do for class string.
I do agree that for complex tasks, string can be easier and safer, and I would agree with its use there. But careful use of C strings makes them just as safe. My personal viewpoint is that new programmers should learn traditional C strings first, because this will allow them to use C strings when its either their only option (C programming) or the best option (time critical code). I also believe knowing how real strings work will allow people to understand how the string class works better, and allow them to use it more effectively.
In fact, about a week ago, I submitted an article to gamedev.net that I wrote about effective use of C strings. Hopefully it''ll get accepted
- Andy Oxfeld
A string class is a wrapper for a C string (or "real string" as I called it). I don''t see how a string class can be considered a real string when it effectively does the same thing you would be doing, and just hides this from you.
No matter how you look at it, string is still not an intrinsic type in C++, and should never be. You don''t need any include files for int, float, etc., but you do for class string.
I do agree that for complex tasks, string can be easier and safer, and I would agree with its use there. But careful use of C strings makes them just as safe. My personal viewpoint is that new programmers should learn traditional C strings first, because this will allow them to use C strings when its either their only option (C programming) or the best option (time critical code). I also believe knowing how real strings work will allow people to understand how the string class works better, and allow them to use it more effectively.
In fact, about a week ago, I submitted an article to gamedev.net that I wrote about effective use of C strings. Hopefully it''ll get accepted
- Andy Oxfeld
quote:
Original post by AndyOxfeld
Learn how real strings (null-terminated character arrays) work, and how to use them effectively. Once you have a solid knowledge of real strings, then you can start using string classes where necessary.
Here is an article (PDF, 40kB) that illustrate my point. Please read it before flaming me. Thanks in advance.
I would actually advise exactly the opposite. A novice C++ programmer has no need to burden himself with the mess of memory management and implementation details that come with C-style strings. He can become productive much faster by learning to use C++ strings properly.
Only when his overall grasp of programming is good enough that his application design and algorithms are sound should he start considering how exactly strings are implemented and see whether a different implementation would suit his needs better.
I am not saying he shouldn't learn about the low-level implementation - it is important - but it is not a priority for a novice. To twist your own words : Once you have a solid knowledge of C++ strings, then you can start using null-terminated strings where necessary.
Knowing how a car is put toghether may help you fine-tune it, but it is not necessary while you're learning to drive. Similarly, using a standard string class - most languages have a built-in string type , C and C++ are notable exceptions - ensure you can learn to program more easily. Having to worry about how strings are put toghether only comes in the way of the learning process.
Edit: typos - and I notice you re-posted in the meantime... ah, well
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on December 14, 2002 10:06:21 AM]
I wasn't trying to put your suggestion down or anything. I was just interested why you made that suggestion 
I agree that most string classes are only a wrapper for c type strings. It could be completely rewritten assembler code (performance) though? Not that it would make much difference to the user, except for speed reasons.
Actually I've written my own string class for a project in university a while ago (the main reason being that I didn't know std::string at that time
). It was quite interesting to do so, and yes it only wraps char*.
It's just that it made the task back then a lot easier, especially when passing strings as arguments and the like, because with char* you would need lots of new/delete, and always ran into troubles, like: when exactly should I delete that thing?
When using the string class the string will just delete itself once the object is destroyed.
I don't know what the real intention of julienX was in the first place. To me it seems like he didn't know what string classes are good for. I guess that would be:
-clean code
-simple to use
Good luck on your article AndyOxfeld
edit: we need typos, lots of typos *cough*
[edited by - Wildfire on December 14, 2002 10:14:11 AM]
I agree that most string classes are only a wrapper for c type strings. It could be completely rewritten assembler code (performance) though? Not that it would make much difference to the user, except for speed reasons.
Actually I've written my own string class for a project in university a while ago (the main reason being that I didn't know std::string at that time
It's just that it made the task back then a lot easier, especially when passing strings as arguments and the like, because with char* you would need lots of new/delete, and always ran into troubles, like: when exactly should I delete that thing?
When using the string class the string will just delete itself once the object is destroyed.
I don't know what the real intention of julienX was in the first place. To me it seems like he didn't know what string classes are good for. I guess that would be:
-clean code
-simple to use
Good luck on your article AndyOxfeld
edit: we need typos, lots of typos *cough*
[edited by - Wildfire on December 14, 2002 10:14:11 AM]
Fruny: Your argument does make sense. I still feel that new programmers should at least learn a LITTLE bit about C strings though, so that if they are reading any code that uses C strings, they will understand what''s going on.
Also, I was reading through that PDF you posted, and found this:
The author then complains that in C, he has to use the number 19, and cannot use the constant max. But, he''s incorrect. The following code would do the same thing and would use the constant max rather than hardcoding the 19 in:
Thank you
Also, I was reading through that PDF you posted, and found this:
const int max = 20
char name[max];
scanf("%19s", name);
The author then complains that in C, he has to use the number 19, and cannot use the constant max. But, he''s incorrect. The following code would do the same thing and would use the constant max rather than hardcoding the 19 in:
const int max = 20
char name[max];
scanf("%*s", max - 1, name);
quote:
Good luck on your article AndyOxfeld
Thank you
quote:
Fruny: Your argument does make sense. I still feel that new programmers should at least learn a LITTLE bit about C strings though, so that if they are reading any code that uses C strings, they will understand what's going on.
Having started with C, having studied compiler implementation, I can easily empathise with that feeling. Knowing why things are as they are helps avoiding feature misuse, but I find this widespread emphasis on learning the low-level stuff first somewhat counter-productive and a source of confusion to beginners.
Algorithms and application design is the primary thing to learn. Without that knowledge, even using assembly language will not save you... and starting with assembly would be just plain wrong in an age when we have (mostly) decent compilers and libaries
Hmm... scanf("%*s", max - 1, name);
*checks K&R* ... interesting...
You are right, though I'd still not call that simple to explain at the same time you're trying to explain what a string is
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on December 14, 2002 10:38:39 AM]
I''m using my own string class too, because this way it''s more flexible then using std::string, since it''s often that i need simple functions (like removing certain chars from a string, special formatting etc.) which are impossible or very hard to do with std::strings, but are easily to add to my own string class based on char*.
And i also think that one should get at least a basic understanding of char* and the functions associated with it, since you often have to deal with api/library functions (win32 etc.) which only work on char* directly. You can of course use string.c_str(), but that''s IMHO not always the best way
Runicsoft -- home of my open source Function Parser and more
And i also think that one should get at least a basic understanding of char* and the functions associated with it, since you often have to deal with api/library functions (win32 etc.) which only work on char* directly. You can of course use string.c_str(), but that''s IMHO not always the best way
Runicsoft -- home of my open source Function Parser and more
I think it really depends on personal preference. Some people prefer to learn the low level stuff first.
I''m glad I was taught about strings through C strings rather than std::string. I think I''d probably have been too lazy to learn C strings once I''d learnt std::string.
Personally, when it is practical, I like to have a good idea of how things work before I use them.
Nowadays I use std::string exclusively. In my opinion, I feel the speed increase (if any) of C strings over std::string would most likely be too small to merit the hassle of having to mess around with char arrays.
I''m glad I was taught about strings through C strings rather than std::string. I think I''d probably have been too lazy to learn C strings once I''d learnt std::string.
Personally, when it is practical, I like to have a good idea of how things work before I use them.
Nowadays I use std::string exclusively. In my opinion, I feel the speed increase (if any) of C strings over std::string would most likely be too small to merit the hassle of having to mess around with char arrays.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement