Řetězec (C++/CLI a C++/CX)

Modul runtime prostředí Windows Runtime a common language runtime představují řetězce jako objekty, jejichž přidělená paměť se spravuje automaticky. To znamená, že není nutné explicitně zahodit paměť pro řetězec, když řetězcová proměnná přestane být obor nebo vaše aplikace končí. Chcete-li označit, že životnost objektu řetězce se má spravovat automaticky, deklarujte typ řetězce s modifikátorem handle-to-object (^).

prostředí Windows Runtime

Architektura prostředí Windows Runtime vyžaduje, aby String byl datový typ umístěn v Platform oboru názvů. Pro usnadnění vašeho pohodlí poskytuje Visual C++ také string datový typ, což je synonymum pro Platform::String, v default oboru názvů.

Syntaxe

// compile with /ZW
using namespace Platform;
using namespace default;
   Platform::String^ MyString1 = "The quick brown fox";
   String^ MyString2 = "jumped over the lazy dog.";
   String^ MyString3 = "Hello, world!";

Požadavky

Možnost kompilátoru: /ZW

CLR (Common Language Runtime)

Při kompilaci /clrpomocí kompilátoru převede řetězcové literály na řetězce typu String. Pokud chcete zachovat zpětnou kompatibilitu s existujícím kódem, existují dvě výjimky:

  • Zpracování výjimek Když je vyvolán řetězcový literál, kompilátor ho zachytí jako řetězcový literál.

  • Odpočty šablon Pokud je řetězcový literál předán jako argument šablony, kompilátor jej nepřevedí na .String Poznámka: Řetězcové literály předané jako obecný argument budou povýšeny na String.

Kompilátor má také integrovanou podporu pro tři operátory, které můžete přepsat a přizpůsobit jejich chování:

  • System::String^ – operátor +( System::String, System::String);

  • System::String^ – operátor +( System::Object, System::String);

  • System::String^ – operátor +( System::String, System::Object);

Při předání String, kompilátor bude v případě potřeby pole a pak zřetězení objektu (s ToString) s řetězcem.

Poznámka

Stříška ("^") označuje, že deklarovaná proměnná je popisovač spravovaného objektu C++/CLI.

Další informace naleznete v tématu Řetězcové a znakové literály.

Požadavky

Možnost kompilátoru: /clr

Příklady

Následující příklad kódu ukazuje zřetězení a porovnávání řetězců.

// string_operators.cpp
// compile with: /clr
// In the following code, the caret ("^") indicates that the
// declared variable is a handle to a C++/CLI managed object.
using namespace System;

int main() {
   String^ a = gcnew String("abc");
   String^ b = "def";   // same as gcnew form
   Object^ c = gcnew String("ghi");

   char d[100] = "abc";

   // variables of System::String returning a System::String
   Console::WriteLine(a + b);
   Console::WriteLine(a + c);
   Console::WriteLine(c + a);

   // accessing a character in the string
   Console::WriteLine(a[2]);

   // concatenation of three System::Strings
   Console::WriteLine(a + b + c);

   // concatenation of a System::String and string literal
   Console::WriteLine(a + "zzz");

   // you can append to a System::String^
   Console::WriteLine(a + 1);
   Console::WriteLine(a + 'a');
   Console::WriteLine(a + 3.1);

   // test System::String^ for equality
   a += b;
   Console::WriteLine(a);
   a = b;
   if (a == b)
      Console::WriteLine("a and b are equal");

   a = "abc";
   if (a != b)
      Console::WriteLine("a and b are not equal");

   // System:String^ and tracking reference
   String^% rstr1 = a;
   Console::WriteLine(rstr1);

   // testing an empty System::String^
   String^ n;
   if (n == nullptr)
      Console::WriteLine("n is empty");
}
abcdef

abcghi

ghiabc

c

abcdefghi

abczzz

abc1

abc97

abc3.1

abcdef

a and b are equal

a and b are not equal

abc

n is empty

Následující ukázka ukazuje, že můžete přetížit operátory poskytované kompilátorem a že kompilátor najde přetížení funkce na String základě typu.

// string_operators_2.cpp
// compile with: /clr
using namespace System;

// a string^ overload will be favored when calling with a String
void Test_Overload(const char * a) {
   Console::WriteLine("const char * a");
}
void Test_Overload(String^ a) {
   Console::WriteLine("String^ a");
}

// overload will be called instead of compiler defined operator
String^ operator +(String^ a, String^ b) {
   return ("overloaded +(String^ a, String^ b)");
}

// overload will be called instead of compiler defined operator
String^ operator +(Object^ a, String^ b) {
   return ("overloaded +(Object^ a, String^ b)");
}

// overload will be called instead of compiler defined operator
String^ operator +(String^ a, Object^ b) {
   return ("overloaded +(String^ a, Object^ b)");
}

int main() {
   String^ a = gcnew String("abc");
   String^ b = "def";   // same as gcnew form
   Object^ c = gcnew String("ghi");

   char d[100] = "abc";

   Console::WriteLine(a + b);
   Console::WriteLine(a + c);
   Console::WriteLine(c + a);

   Test_Overload("hello");
   Test_Overload(d);
}
overloaded +(String^ a, String^ b)

overloaded +(String^ a, Object^ b)

overloaded +(Object^ a, String^ b)

String^ a

const char * a

Následující ukázka ukazuje, že kompilátor rozlišuje mezi nativními řetězci a String řetězci.

// string_operators_3.cpp
// compile with: /clr
using namespace System;
int func() {
   throw "simple string";   // const char *
};

int func2() {
   throw "string" + "string";   // returns System::String
};

template<typename T>
void func3(T t) {
   Console::WriteLine(T::typeid);
}

int main() {
   try {
      func();
   }
   catch(char * e) {
      Console::WriteLine("char *");
   }

   try {
      func2();
   }
   catch(String^ str) {
      Console::WriteLine("String^ str");
   }

   func3("string");   // const char *
   func3("string" + "string");   // returns System::String
}
char *

String^ str

System.SByte*

System.String

Viz také

Přípony komponent pro .NET a UPW
Řetězcové a znakové literály
/clr (kompilace modulu Common Language Runtime)