简体   繁体   中英

C++ meaning of String^

I'm trying to make a getter for a Label (Windows Form).

In myForm.h :

public: String^ getLabel1() { return label1->Text; };

But when I try to print it, it won't compile!

In myForm.cpp :

void Main(array<String^>^args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);

    Test::MyForm form;
    Application::Run(%form);

    form.setLabel1();
    std::cout << form.getLabel1() << std::endl;
}

I guess I return the wrong type (String^), but i can't find anything on the '^' character, and when I try to return a std::string , I can't compile it! Can someone help me?

String^ is "Reference to a GC object" - an object that exists in the Managed Heap (ie a ".NET object"). Compared to String* (a pointer to a System::String CLR object or std::string (a C++ standard-library string local).

The C++ STL cout stream does not support outputting System::String , you must either first marshal it to a compatible type, or use Console::WriteLine instead.

To re-iterate what sp2danny said in the comments, this is not C++, this is C++/CLI, which can be considered a C++-esque syntax for CIL+CLR alternative to C# with the benefit of being able to use existing C++ code in source-form, including the STL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM