CStringArray vs. CArray<CString>
-
Hi, I am wondering what might be the advantage of
CStringArray
overCArray<CString, LPCTSTR>
. The background is that I am planning to implement a CStringArray-like template class which is either explicitly ANSI or explicitly Unicode, and would like to implement it asCArray<CStringA, LPCSTR>
andCArray<CStringW, LPCWSTR>
, respectively. Thank you for any comments. Hans -
Hi, I am wondering what might be the advantage of
CStringArray
overCArray<CString, LPCTSTR>
. The background is that I am planning to implement a CStringArray-like template class which is either explicitly ANSI or explicitly Unicode, and would like to implement it asCArray<CStringA, LPCSTR>
andCArray<CStringW, LPCWSTR>
, respectively. Thank you for any comments. HansI have never mastered the usage of MFC container classes but I have an advice: support only unicode unless you are forced to support ansi. WinNT uses unicode/UTF16 inside, the codepaging/ansi api is just heritage from older windows versions. Supporting both is just a waste of time and unecessary complication if you don't have to support Win9x. EDIT: Regarding the difference between
CStringArray
andCArray<CString...>
I have a tip: The old versions of MFC didn't contain any templated containers, those mfc versions had only some non-template containers for a few types andCStringArray
was one of them (among the others likeCByteArray
). I guessCStringArray
exists just because of backward compatibility.CString
didn't have reference counting in some older versions of MFC so in theory it is possible for a specializedCStringArray
to reallocate the array memory without copying strings ifCStringArray
knows the internal implementation ofCString
, but a templatedCArray
has to work with copying/deleting. With reference counting a general CArray can do a relatively good job and with today's move ctors it can be even better if MFC supports it. -
I have never mastered the usage of MFC container classes but I have an advice: support only unicode unless you are forced to support ansi. WinNT uses unicode/UTF16 inside, the codepaging/ansi api is just heritage from older windows versions. Supporting both is just a waste of time and unecessary complication if you don't have to support Win9x. EDIT: Regarding the difference between
CStringArray
andCArray<CString...>
I have a tip: The old versions of MFC didn't contain any templated containers, those mfc versions had only some non-template containers for a few types andCStringArray
was one of them (among the others likeCByteArray
). I guessCStringArray
exists just because of backward compatibility.CString
didn't have reference counting in some older versions of MFC so in theory it is possible for a specializedCStringArray
to reallocate the array memory without copying strings ifCStringArray
knows the internal implementation ofCString
, but a templatedCArray
has to work with copying/deleting. With reference counting a general CArray can do a relatively good job and with today's move ctors it can be even better if MFC supports it.There are situations when I must support ANSI, for example when it comes to dealing with ANSI text files or serial protocols. I want to have the CStringArray functionality available for the must-be-ANSI situations while generally compiling for Unicode. Btw, I find MFC containers quite useful (unless, of course, you are using STL). The documentation is quite good, and you can debug-step through the source code.
-
There are situations when I must support ANSI, for example when it comes to dealing with ANSI text files or serial protocols. I want to have the CStringArray functionality available for the must-be-ANSI situations while generally compiling for Unicode. Btw, I find MFC containers quite useful (unless, of course, you are using STL). The documentation is quite good, and you can debug-step through the source code.
Do you receive single bytes/ascii or really ansi on the serial port? Many people confuse ansi with ascii or latin1. Similarly, many windows coders confuse utf16 with unicode (they are two different things) and wchar_t with a unicode character. Can you clearly separate these concepts because if not I can write you a very little mini tutorial about these here. I want to convince you that compiling your program only for unicode is probably enough. Btw, what is on the other end of the serial port and what kind of codepage do you use in the "ansi" text for communication, is the text bare ascii (characters below chr(128))? Containers: No matter what containers you use, its easy to debug either implementation. One of the project I'm currently working on contains own container implementations (array, hashmap, ...) and even these containers are debuggable if you can adjust the autoexp.dat file of your visual studio accordingly to teach the debugger how to get out items from the container. When I choose containers I consider 2 aspects: portability to different platforms and whether I need hardcore performance optimization later in the project or not. If we consider portability then stl is a much better choice than mfc containers, thats why I never really used mfc containers. Writing your own containers isn't practical in 99% of the projects.
-
Do you receive single bytes/ascii or really ansi on the serial port? Many people confuse ansi with ascii or latin1. Similarly, many windows coders confuse utf16 with unicode (they are two different things) and wchar_t with a unicode character. Can you clearly separate these concepts because if not I can write you a very little mini tutorial about these here. I want to convince you that compiling your program only for unicode is probably enough. Btw, what is on the other end of the serial port and what kind of codepage do you use in the "ansi" text for communication, is the text bare ascii (characters below chr(128))? Containers: No matter what containers you use, its easy to debug either implementation. One of the project I'm currently working on contains own container implementations (array, hashmap, ...) and even these containers are debuggable if you can adjust the autoexp.dat file of your visual studio accordingly to teach the debugger how to get out items from the container. When I choose containers I consider 2 aspects: portability to different platforms and whether I need hardcore performance optimization later in the project or not. If we consider portability then stl is a much better choice than mfc containers, thats why I never really used mfc containers. Writing your own containers isn't practical in 99% of the projects.
Well in fact I should use proper terminology. When I said "ANSI" I meant 8-bit character stuff. When I said "Unicode" I meant 16-bit characters. Speaking of Unicode code points, mostly in the range 0000 through 007f. In other words, ASCII characters in different representations. But this discussion is getting beside the point. My question was, what is the advantage of CStringArray over CArray<CString, LPCTSTR>? Rephrase: Why is there a CStringArray class when CArray<CString, LPCTSTR> has the same functionality? Or does it not?
-
Well in fact I should use proper terminology. When I said "ANSI" I meant 8-bit character stuff. When I said "Unicode" I meant 16-bit characters. Speaking of Unicode code points, mostly in the range 0000 through 007f. In other words, ASCII characters in different representations. But this discussion is getting beside the point. My question was, what is the advantage of CStringArray over CArray<CString, LPCTSTR>? Rephrase: Why is there a CStringArray class when CArray<CString, LPCTSTR> has the same functionality? Or does it not?
Here is a nice page that can help you in choosing between the two options: http://msdn.microsoft.com/en-us/library/y1z022s1%28v=vs.71%29.aspx[^]. In my previous post I mentioned that in the early days of MFC there were no template usage in the mfc library so they created several array implementation for some basic types like WORD, CString, Int,... Later they introduced templated classes like CArray<> but they kept the earlier container classes as well for backward compatibility so as to keep old legacy code compilable. Today you can use either of them but probably the CArray is preferred in new code. I would compile only a single unicode-aware version from my program and I would convert my strings to ascii only before sending them over serial connection and converting them back after receiving the string from serial but of course this is your choice. If you used utf-8 to represent strings then the conversion would be unnecessary - with utf8 you should convert between utf8 and utf16 only when you call winapi that has string parameters and/or return values. I like using utf-8 internally because its compatible with ascii, unicode aware, and its very easy to port to unix like systems at the same time.
-
Here is a nice page that can help you in choosing between the two options: http://msdn.microsoft.com/en-us/library/y1z022s1%28v=vs.71%29.aspx[^]. In my previous post I mentioned that in the early days of MFC there were no template usage in the mfc library so they created several array implementation for some basic types like WORD, CString, Int,... Later they introduced templated classes like CArray<> but they kept the earlier container classes as well for backward compatibility so as to keep old legacy code compilable. Today you can use either of them but probably the CArray is preferred in new code. I would compile only a single unicode-aware version from my program and I would convert my strings to ascii only before sending them over serial connection and converting them back after receiving the string from serial but of course this is your choice. If you used utf-8 to represent strings then the conversion would be unnecessary - with utf8 you should convert between utf8 and utf16 only when you call winapi that has string parameters and/or return values. I like using utf-8 internally because its compatible with ascii, unicode aware, and its very easy to port to unix like systems at the same time.
Thanks for that link... actually it doesn't tell me why I should use one or the other. There table in that page lists two differences between CArray and CStringArray: 1. The former uses templates while the latter doesn't - well, that is obvious. 2. CArray isn't type safe while CStringArray is - but I fail to see why. A CArray<CString, LPCTSTR> can only contain CString objects, so what should be type unsafe? So actually, my question remains open :-( Regarding your advice, I have to deal with tons of old code. Many parts of that code work well with Unicode, but I know some places that confuse bytes with characters, and I sure don't know all of them, and I just don't have the time to clean everything up. I will have to live with a mix of 16 and 8 bit character strings. Besides, it is a pity that there is no built-in or MFC UTF-8 string type in C++. I love Perl for that.
-
Thanks for that link... actually it doesn't tell me why I should use one or the other. There table in that page lists two differences between CArray and CStringArray: 1. The former uses templates while the latter doesn't - well, that is obvious. 2. CArray isn't type safe while CStringArray is - but I fail to see why. A CArray<CString, LPCTSTR> can only contain CString objects, so what should be type unsafe? So actually, my question remains open :-( Regarding your advice, I have to deal with tons of old code. Many parts of that code work well with Unicode, but I know some places that confuse bytes with characters, and I sure don't know all of them, and I just don't have the time to clean everything up. I will have to live with a mix of 16 and 8 bit character strings. Besides, it is a pity that there is no built-in or MFC UTF-8 string type in C++. I love Perl for that.
I'm pretty sure that CStringArray and CArray<CString> provide the same functionality. If later you find out then its still an easy search and replace. I wouldn't worry about this choice. I myself prefer templates as in some cases templated containers are easier to handle (from other templates) than specific types like CStringArray.
-
Hi, I am wondering what might be the advantage of
CStringArray
overCArray<CString, LPCTSTR>
. The background is that I am planning to implement a CStringArray-like template class which is either explicitly ANSI or explicitly Unicode, and would like to implement it asCArray<CStringA, LPCSTR>
andCArray<CStringW, LPCWSTR>
, respectively. Thank you for any comments. HansThey provide the same functionality, though CStringArray is faster to add strings (it is specialized, so that makes sense.)