Sdílet prostřednictvím


String (rozšíření komponent C++)

Kompilátor Visual C++ podporuje řetězce, což jsou objekty, které reprezentují text jako sekvenci znaků.Visual C++ podporuje proměnné řetězců, jejichž hodnota je implicitní, a literály, jejichž hodnota je explicitně uvedený řetězec.

Všechny moduly runtime

Prostředí Windows Runtime and modul CLR (Common Language Runtime) reprezentují řetězce jako objekty, jejichž alokována paměť je automaticky spravována.Tedy, není nutné explicitně zahazovat paměť pro řetězec, jakmile proměnná řetězce vypadne z rozsahu aplikace nebo je aplikace ukončena.Pro určení, že doba života objektu řetězce je spravována automaticky, je nutné deklarovat typ řetězce s modifikátorem handle-to-object (^).

Prostředí Windows Runtime

Architektura prostředí Windows Runtime vyžaduje, aby Visual C++ implementovalo datový typ String ve jmenném prostoru Platform.Pro lepší použitelnost, Visual C++ také poskytuje datový typ string, který je synonymum pro Platform::String ve jmenném prostoru default.

Syntax

// 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!";

Poznámky

Další informace o řetězcích a příklady s řetězci naleznete v tématu Platform::String, std::wstring a literálů (Platform).

Požadavky

Možnost kompilátoru: /ZW

Common Language Runtime

Téma popisuje, jak kompilátor Visual C++ zpracovává literály řetězců při spuštění s možností kompilátoru /clr.Pro použití /clr je nutné použít modul CLR (Common Language Runtime), C++/CLI syntaxi a spravované objekty.Další informace o /clr naleznete v tématu /clr (Common Language Runtime).

Při kompilaci s /clr, kompilátor převede literály řetězců na řetězce typu String.Pro zachování zpětné kompatibility s existujícím kódem existují dvě výjimky:

  • Zpracování výjimek.Pokud je vyvolán literál řetězce, kompilátor jej zachytí jako literál řetězce.

  • Odvození šablon.Pokud je literál řetězce předán jako argument šablony, kompilátor jej nepřevede na String.Nutno poznamenat, že literál řetězce předaný jako argument obecného typu bude povýšen na String.

Kompilátor má také zabudovanou podporu pro tři operátory, které je možné přepsat, pro přizpůsobení jejich chování:

  • System::String ^ operator +( System::String, System::String);

  • System::String ^ operator +( System::Object, System::String);

  • System::String ^ operator +( System::String, System::Object);

Při předání String, provede, v případě nutnosti, kompilátor zabalení a následně spojí objekt (pomocí ToString) s řetězcem.

Při kompilaci s /clr:oldSyntax, literály řetězců nebudou převedeny na String.

[!POZNÁMKA]

Kurzor ("^") ukazuje, že deklarovaná proměnná je popisovač spravovaného objektu C++/CLI.

Další informace naleznete v tématu Textové literály jazyka C++.

Požadavky

Možnost kompilátoru: /clr

Příklady

Příklad

Následující ukázka kódu demonstruje spojování 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");
}

Výsledek

  
  
  
  
  
  
  
  
  
  
  
  
  
  

Příklad

Následující příklad ukazuje, že je možné přetížit kompilátorem poskytované operátory, a že kompilátor najde přetížení funkce založené na typu String.

// 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);
}

Výsledek

  
  
  
  
  

Příklad

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

// 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
}

Výsledek

  
  
  
  

Viz také

Referenční dokumentace

Textové literály jazyka C++

/clr (Common Language Runtime)

Koncepty

Rozšíření komponent pro platformy běhového prostředí