user4521750
user4521750

Reputation:

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?

Upvotes: 2

Views: 89

Answers (1)

Dai
Dai

Reputation: 155290

String^ is "Reference to a GC object" - an object that exists in the Managed Heap (i.e. 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.

Upvotes: 9

Related Questions