Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CStringArray vs. CArray<CString>

CStringArray vs. CArray<CString>

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studio
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hans sch
    wrote on last edited by
    #1

    Hi, I am wondering what might be the advantage of CStringArray over CArray<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 as CArray<CStringA, LPCSTR> and CArray<CStringW, LPCWSTR>, respectively. Thank you for any comments. Hans

    P J 2 Replies Last reply
    0
    • H hans sch

      Hi, I am wondering what might be the advantage of CStringArray over CArray<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 as CArray<CStringA, LPCSTR> and CArray<CStringW, LPCWSTR>, respectively. Thank you for any comments. Hans

      P Offline
      P Offline
      pasztorpisti
      wrote on last edited by
      #2

      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 and CArray<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 and CStringArray was one of them (among the others like CByteArray). I guess CStringArray 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 specialized CStringArray to reallocate the array memory without copying strings if CStringArray knows the internal implementation of CString, but a templated CArray 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.

      H 1 Reply Last reply
      0
      • P pasztorpisti

        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 and CArray<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 and CStringArray was one of them (among the others like CByteArray). I guess CStringArray 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 specialized CStringArray to reallocate the array memory without copying strings if CStringArray knows the internal implementation of CString, but a templated CArray 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.

        H Offline
        H Offline
        hans sch
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • H hans sch

          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.

          P Offline
          P Offline
          pasztorpisti
          wrote on last edited by
          #4

          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.

          H 1 Reply Last reply
          0
          • P pasztorpisti

            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.

            H Offline
            H Offline
            hans sch
            wrote on last edited by
            #5

            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?

            P 1 Reply Last reply
            0
            • H hans sch

              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?

              P Offline
              P Offline
              pasztorpisti
              wrote on last edited by
              #6

              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.

              H 1 Reply Last reply
              0
              • P pasztorpisti

                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.

                H Offline
                H Offline
                hans sch
                wrote on last edited by
                #7

                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.

                P 1 Reply Last reply
                0
                • H hans sch

                  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.

                  P Offline
                  P Offline
                  pasztorpisti
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • H hans sch

                    Hi, I am wondering what might be the advantage of CStringArray over CArray<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 as CArray<CStringA, LPCSTR> and CArray<CStringW, LPCWSTR>, respectively. Thank you for any comments. Hans

                    J Offline
                    J Offline
                    Joe Woodbury
                    wrote on last edited by
                    #9

                    They provide the same functionality, though CStringArray is faster to add strings (it is specialized, so that makes sense.)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups