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. How can CString be converted to LPCTSTR without any operator()?

How can CString be converted to LPCTSTR without any operator()?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionlearning
7 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.
  • D Offline
    D Offline
    Dean Seo
    wrote on last edited by
    #1

    Hi, I have been looking for the answer to this for a long time using google and everything. I may have found some postings, but those didn't fully help me to understand this. So I checked CStringT and CSimpleStringT, and what I found is that there is only this, "operator PCXSTR()" This seems to say that CString, which is simply CSTringT, can be converted ONLY to PCXSTR. However, this code below works fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = _str; // success ---------------------------------------------- Based on the fact that there is one operator method, which is operator PCXSTR(), I guess the complier just casts PCXSTR into LPCTSTR automatically, because this code below again works also fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = (LPCTSTR)_str; // success ---------------------------------------------- Is this correct? Type PCXSTR is just converted to type LPCTSTR, which makes it seems that CString is also converted to LPCTSTR as if CString has LPCTSTR operator? (I am, of course, talking about this in UNICODE circumstance by the way) I think I know that I misunderstand something that could be critical, but I don't even know where to start to solve this. Thanks in advance.

    R 1 Reply Last reply
    0
    • D Dean Seo

      Hi, I have been looking for the answer to this for a long time using google and everything. I may have found some postings, but those didn't fully help me to understand this. So I checked CStringT and CSimpleStringT, and what I found is that there is only this, "operator PCXSTR()" This seems to say that CString, which is simply CSTringT, can be converted ONLY to PCXSTR. However, this code below works fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = _str; // success ---------------------------------------------- Based on the fact that there is one operator method, which is operator PCXSTR(), I guess the complier just casts PCXSTR into LPCTSTR automatically, because this code below again works also fine. ---------------------------------------------- CString m_str = TEXT("abcd"); LPCTSTR m_lpctstr = (LPCTSTR)_str; // success ---------------------------------------------- Is this correct? Type PCXSTR is just converted to type LPCTSTR, which makes it seems that CString is also converted to LPCTSTR as if CString has LPCTSTR operator? (I am, of course, talking about this in UNICODE circumstance by the way) I think I know that I misunderstand something that could be critical, but I don't even know where to start to solve this. Thanks in advance.

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Dean Seo wrote:

      as if CString has LPCTSTR operator

      Actually, it does have one: CString::operator LPCTSTR[^]

      "Real men drive manual transmission" - Rajesh.

      M 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        Dean Seo wrote:

        as if CString has LPCTSTR operator

        Actually, it does have one: CString::operator LPCTSTR[^]

        "Real men drive manual transmission" - Rajesh.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Rajesh R Subramanian wrote:

        Actually, it does have one

        In Visual Studio 6 :) This changed when CString was split off from MFC...

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        R 1 Reply Last reply
        0
        • M Mark Salsbery

          Rajesh R Subramanian wrote:

          Actually, it does have one

          In Visual Studio 6 :) This changed when CString was split off from MFC...

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          You're correct. However, CSimpleStringT does have a PCXSTR operator defined (which when compiling for Unicode should be LPCWSTR).

          "Real men drive manual transmission" - Rajesh.

          D 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            You're correct. However, CSimpleStringT does have a PCXSTR operator defined (which when compiling for Unicode should be LPCWSTR).

            "Real men drive manual transmission" - Rajesh.

            D Offline
            D Offline
            Dean Seo
            wrote on last edited by
            #5

            Thanks for answering my question. This helps me a lot. So consequently, in UNICODE circumstance, the complier automatically casts PCXSTR to LPCWSTR, or LPCTSTR? Also, that is why CSimpleStringT only needs operator PCXSTR()? Thank you.

            R 1 Reply Last reply
            0
            • D Dean Seo

              Thanks for answering my question. This helps me a lot. So consequently, in UNICODE circumstance, the complier automatically casts PCXSTR to LPCWSTR, or LPCTSTR? Also, that is why CSimpleStringT only needs operator PCXSTR()? Thank you.

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              Yes, Dean. You're correct. Excerpt from the header atlsimpstr.h

              template< typename BaseType = char > //_MBCS build
              class ChTraitsBase
              {
              public:
              typedef char XCHAR;
              typedef LPSTR PXSTR;
              typedef LPCSTR PCXSTR; //First definition here
              typedef wchar_t YCHAR;
              typedef LPWSTR PYSTR;
              typedef LPCWSTR PCYSTR;
              };

              template<>
              class ChTraitsBase< wchar_t > //Unicode build
              {
              public:
              typedef wchar_t XCHAR;
              typedef LPWSTR PXSTR;
              typedef LPCWSTR PCXSTR; //Second definition here
              typedef char YCHAR;
              typedef LPSTR PYSTR;
              typedef LPCSTR PCYSTR;
              };

              "Real men drive manual transmission" - Rajesh.

              D 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                Yes, Dean. You're correct. Excerpt from the header atlsimpstr.h

                template< typename BaseType = char > //_MBCS build
                class ChTraitsBase
                {
                public:
                typedef char XCHAR;
                typedef LPSTR PXSTR;
                typedef LPCSTR PCXSTR; //First definition here
                typedef wchar_t YCHAR;
                typedef LPWSTR PYSTR;
                typedef LPCWSTR PCYSTR;
                };

                template<>
                class ChTraitsBase< wchar_t > //Unicode build
                {
                public:
                typedef wchar_t XCHAR;
                typedef LPWSTR PXSTR;
                typedef LPCWSTR PCXSTR; //Second definition here
                typedef char YCHAR;
                typedef LPSTR PYSTR;
                typedef LPCSTR PCYSTR;
                };

                "Real men drive manual transmission" - Rajesh.

                D Offline
                D Offline
                Dean Seo
                wrote on last edited by
                #7

                Thanks. This helps me a lot and I now feel that I understand MFC more than before.

                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