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. Managed C++/CLI
  4. Converting from std::string to System::String^

Converting from std::string to System::String^

Scheduled Pinned Locked Moved Managed C++/CLI
c++data-structureshelpquestion
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.
  • F Offline
    F Offline
    flippydeflippydebop
    wrote on last edited by
    #1

    Hello everyone... Im just starting a new project using C++/CLI. Can some one please tell me why this code does not work?

    std::string nativeStr = "Hello World";
    String^ managedStr = nativeStr.c_str(); // error C2440: 'initializing' : cannot convert from 'const char *' to 'System::String ^'

    but this code does work

    std::string nativeStr = "Hello World";
    String^ s6 = gcnew String(nativeStr.c_str()); // works!

    howevet i want to be able to create my managedStr on the stack, and would expect this to work.. Can anybody tell me where i am going wrong

    L M 2 Replies Last reply
    0
    • F flippydeflippydebop

      Hello everyone... Im just starting a new project using C++/CLI. Can some one please tell me why this code does not work?

      std::string nativeStr = "Hello World";
      String^ managedStr = nativeStr.c_str(); // error C2440: 'initializing' : cannot convert from 'const char *' to 'System::String ^'

      but this code does work

      std::string nativeStr = "Hello World";
      String^ s6 = gcnew String(nativeStr.c_str()); // works!

      howevet i want to be able to create my managedStr on the stack, and would expect this to work.. Can anybody tell me where i am going wrong

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      flippydeflippydebop wrote:

      i want to be able to create my managedStr on the stack, and would expect this to work

      Then you don't understand how the .NET Platform works. Try reading some introductory programming articles on MSDN[^] Actually there might even be some here on CodeProject if you care to look.

      led mike

      1 Reply Last reply
      0
      • F flippydeflippydebop

        Hello everyone... Im just starting a new project using C++/CLI. Can some one please tell me why this code does not work?

        std::string nativeStr = "Hello World";
        String^ managedStr = nativeStr.c_str(); // error C2440: 'initializing' : cannot convert from 'const char *' to 'System::String ^'

        but this code does work

        std::string nativeStr = "Hello World";
        String^ s6 = gcnew String(nativeStr.c_str()); // works!

        howevet i want to be able to create my managedStr on the stack, and would expect this to work.. Can anybody tell me where i am going wrong

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

        flippydeflippydebop wrote:

        howevet i want to be able to create my managedStr on the stack, and would expect this to work..

        LedMike has the correct answer. To add...creating a managed object on the stack is a redundant/silly concept. It's all about scope here. Creating it the way you've shown in the working sample gives it the same scope as a stack variable. Mark

        "If you can dodge a wrench, you can dodge a ball."

        F 1 Reply Last reply
        0
        • M Mark Salsbery

          flippydeflippydebop wrote:

          howevet i want to be able to create my managedStr on the stack, and would expect this to work..

          LedMike has the correct answer. To add...creating a managed object on the stack is a redundant/silly concept. It's all about scope here. Creating it the way you've shown in the working sample gives it the same scope as a stack variable. Mark

          "If you can dodge a wrench, you can dodge a ball."

          F Offline
          F Offline
          flippydeflippydebop
          wrote on last edited by
          #4

          well hang on then guys... Why does this:

          System::String^ myStr = "hello"

          Work fine even though i do not use the gcnew operator to allocate the string 'myStr' on the heap? What i am saying is that i know in the .net world everything is allocated on the heap and that the above statement, even though it appears to reside on the stack is probably being allocated on the heap.. Now if the compiler lets me write code like above, then why wont it let me write code like this:

          String^ managedStr = nativeStr.c_str();

          The above line fails.. Instead in this instance i have to explicitly allocate the variable managedStr on the heap like so: String^ s6 = gcnew String(nativeStr.c_str()); // works!

          M L 2 Replies Last reply
          0
          • F flippydeflippydebop

            well hang on then guys... Why does this:

            System::String^ myStr = "hello"

            Work fine even though i do not use the gcnew operator to allocate the string 'myStr' on the heap? What i am saying is that i know in the .net world everything is allocated on the heap and that the above statement, even though it appears to reside on the stack is probably being allocated on the heap.. Now if the compiler lets me write code like above, then why wont it let me write code like this:

            String^ managedStr = nativeStr.c_str();

            The above line fails.. Instead in this instance i have to explicitly allocate the variable managedStr on the heap like so: String^ s6 = gcnew String(nativeStr.c_str()); // works!

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

            When you do this

            System::String^ myStr = "hello"

            myStr gets a reference to a pointer that already exists in the app's (assembly's) user strings metadata section. When you do this

            String^ managedStr = nativeStr.c_str();

            a System::String constructor is required. To use the constructor the object needs to be created on the managed heap. I tracked down a better description than I could possibly give: Scroll to the second question/answer here[^] Hope that helps! Mark

            "If you can dodge a wrench, you can dodge a ball."

            1 Reply Last reply
            0
            • F flippydeflippydebop

              well hang on then guys... Why does this:

              System::String^ myStr = "hello"

              Work fine even though i do not use the gcnew operator to allocate the string 'myStr' on the heap? What i am saying is that i know in the .net world everything is allocated on the heap and that the above statement, even though it appears to reside on the stack is probably being allocated on the heap.. Now if the compiler lets me write code like above, then why wont it let me write code like this:

              String^ managedStr = nativeStr.c_str();

              The above line fails.. Instead in this instance i have to explicitly allocate the variable managedStr on the heap like so: String^ s6 = gcnew String(nativeStr.c_str()); // works!

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Yes because "hello" is treated as the type (System.String) by the CLI compiler. Look at the MSIL code to see the difference. Also note that: System::String^ myStr = (const char*)"hello"; does not work. See Stan Lipman's blog[^] The insight to resolve this is to realize that the dual citizenship of a string literal applies to its fundamental type, not to its set of trivial conversions. In effect, under C++/CLI, the underlying type of a string literal such as "Pooh" is both const char[5] (its native inheritance) and System::String (its managed underlying unified type). Under C++/CLI, the string literal is an exact match to System::String and the trivial conversion to const char* is not considered. That is, under the revised C++/CLI language specification, the ambiguity has been resolved in favor of System::String

              led mike

              F 1 Reply Last reply
              0
              • L led mike

                Yes because "hello" is treated as the type (System.String) by the CLI compiler. Look at the MSIL code to see the difference. Also note that: System::String^ myStr = (const char*)"hello"; does not work. See Stan Lipman's blog[^] The insight to resolve this is to realize that the dual citizenship of a string literal applies to its fundamental type, not to its set of trivial conversions. In effect, under C++/CLI, the underlying type of a string literal such as "Pooh" is both const char[5] (its native inheritance) and System::String (its managed underlying unified type). Under C++/CLI, the string literal is an exact match to System::String and the trivial conversion to const char* is not considered. That is, under the revised C++/CLI language specification, the ambiguity has been resolved in favor of System::String

                led mike

                F Offline
                F Offline
                flippydeflippydebop
                wrote on last edited by
                #7

                ahh-haaaa... i see (said the blind man). Well, guys it would seem i need to do some more reading of the subject material. Thankyou both for taking the time to reply. :)

                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