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. CString Vs std::string

CString Vs std::string

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioquestion
4 Posts 3 Posters 1 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.
  • J Offline
    J Offline
    John theKing
    wrote on last edited by
    #1

    Hi !! There is a AllocSysString member in CString class. I want to give the same functionality to the string represented by std::string. Is it possible ???

    J 1 Reply Last reply
    0
    • J John theKing

      Hi !! There is a AllocSysString member in CString class. I want to give the same functionality to the string represented by std::string. Is it possible ???

      J Offline
      J Offline
      Jim Crafton
      wrote on last edited by
      #2

      AllocSysString is used for BSTR's so just off the top of my head...

      #include <comdef.h>

      class MyString {
      public:
      std::string& get() {
      return m_str;
      }

      operator BSTR () {
      _bstr_t tmp;
      tmp = m_str.c_str();
      return tmp.copy();
      }
      protected:
      std::string m_str;
      };

      this is pretty crude, but may give you some ideas... ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!

      A 1 Reply Last reply
      0
      • J Jim Crafton

        AllocSysString is used for BSTR's so just off the top of my head...

        #include <comdef.h>

        class MyString {
        public:
        std::string& get() {
        return m_str;
        }

        operator BSTR () {
        _bstr_t tmp;
        tmp = m_str.c_str();
        return tmp.copy();
        }
        protected:
        std::string m_str;
        };

        this is pretty crude, but may give you some ideas... ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        Thanks for your response. Can you please also tell me that how can i use the operator BSTR of your class. Example if i have string std::string strName = "Jim"; BSTR bstrName; Now please tell me how i use your class to copy my std::string in BSTR. Thanks.

        J 1 Reply Last reply
        0
        • A Anonymous

          Thanks for your response. Can you please also tell me that how can i use the operator BSTR of your class. Example if i have string std::string strName = "Jim"; BSTR bstrName; Now please tell me how i use your class to copy my std::string in BSTR. Thanks.

          J Offline
          J Offline
          Jim Crafton
          wrote on last edited by
          #4

          std::string strName = "Jim";
          BSTR bstrName;
          MyString s;
          s.get() = strName;

          bstrName = s;

          Thats it. But before you go using it, you should definitely read up on C++ operator overloads and conversion operators. COnversion operators are what allow you to do what we did above in the line: bstrName = s; Basically the compiler sees you have a conversion operator for the type of the assignment (in our case a BSTR) and then calls it for you. ¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!

          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