文字列 (C++ コンポーネント拡張)

Visual C コンパイラ サポート文字列、ある一連の文字として文字列を表すオブジェクト。Visual C には、値は暗黙的に、文字列変数、およびリテラルの値は、明示的な引用符で囲まれた文字列は、サポートしています。

すべてのランタイム

Windows ランタイムおよび共通言語ランタイムが割り当てられているメモリを自動的に管理するオブジェクトとして文字列を表します。つまり、文字列変数が範囲外か、アプリケーションが終了すると、文字列用のメモリを明示的に破棄する必要はありません。String オブジェクトの有効期間が自動的に管理するためにあることを示すには、文字列型を宣言、 ハンドルをオブジェクト (^) 修飾子。

Windows ランタイム

Windows ランタイムのアーキテクチャを実装するには、Visual C を必要とする、 Stringデータ型で、 Platform名前空間。必要に応じて、Visual C も提供、 stringデータ型は、シノニムをPlatform::Stringで、 default名前空間。

ms177218.collapse_all(ja-jp,VS.110).gif構文

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

ms177218.collapse_all(ja-jp,VS.110).gif解説

詳細と例については、文字列についてを参照してください。プラットフォーム:: 文字列、 std:: wstring、リテラル (プラットフォーム)

ms177218.collapse_all(ja-jp,VS.110).gif要件

コンパイラ オプション:/ZW

共通言語ランタイム

このトピックを使用して実行する場合に、Visual C コンパイラは、文字列リテラルを処理する方法を説明します、 /clrコンパイラ オプション。使用する**/clrも、共通言語ランタイム (CLR) は、C + を使用する必要があります + CLI 構文とオブジェクトを管理します。/clr** の詳細については、「/clr (共通言語ランタイムのコンパイル)」を参照してください。

コンパイルすると**/clr**、コンパイラが型の文字列にリテラル文字列を変換しますString。既存のコードがあるとの下位互換性を保持するには、2 つの例外です。

  • 例外処理します。リテラル文字列がスローされると、コンパイラはリテラル文字列としてをキャッチします。

  • テンプレートの控除。文字列リテラルは、テンプレート引数として渡されるときは、コンパイラに変換されません、 String。メモは、汎用引数として渡された文字列のリテラルのプロモートをString

コンパイラは、その動作をカスタマイズするオーバーライドできます 3 人のオペレーターの組み込みサポートがあります。

  • System::string ^ 演算子 + (system::string、system::string)。

  • System::string ^ 演算子 + (:object、system::string)。

  • System::string ^ 演算子 + system: (:string、:object)。

渡されると、 String、コンパイラは、必要な場合は、ボックスし、(ToString) をオブジェクトと文字列を連結します。

コンパイルすると**/clr:oldSyntax**、文字列リテラル変換されませんをString

[!メモ]

カレット ("^") で宣言された変数 C + へのハンドルであることを示します C++/cli CLI マネージ オブジェクト。

詳細については、「C++ の文字列リテラル」を参照してください。

ms177218.collapse_all(ja-jp,VS.110).gif要件

コンパイラ オプション:/clr

ms177218.collapse_all(ja-jp,VS.110).gif

連結して文字列を比較するコード例を次に示します。

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

出力

  
  
  
  
  
  
  
  
  
  
  
  
  
  

次の例では、コンパイラが提供の演算子をオーバー ロードできます、およびコンパイラを見つけ出し、に基づいて、関数のオーバー ロードを示しています、 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);
}

出力

  
  
  
  
  

次の例では、コンパイラの間で、ネイティブ文字列を区別することを示していますと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
}

出力

  
  
  
  

参照

関連項目

C++ の文字列リテラル

/clr (共通言語ランタイムのコンパイル)

概念

ランタイム プラットフォームのコンポーネントの拡張機能