次の方法で共有


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

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

すべてのランタイム

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

Windows ランタイム

Windows ランタイムのアーキテクチャでは、Visual C++ で Platform 名前空間の String データ型を実装する必要があります。 これを簡単にするために、Visual C++ では、Platform::String のシノニムとして、default 名前空間に用意されている string データ型を使用することもできます。

構文

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

解説

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

要件

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

共通言語ランタイム

ここでは、/clr コンパイラ オプションを使用して文字列リテラルを実行したときに Visual C++ コンパイラでどのように処理されるかについて説明します。 /clr を使用するには、共通言語ランタイム (CLR) に加え、C++/CLI の構文とマネージ オブジェクトも使用する必要があります。 /clr の詳細については、「/clr (共通言語ランタイムのコンパイル)」を参照してください。

/clr を指定してコンパイルする場合、文字列リテラルは String 型の文字列に変換されます。 これには、既存のコードとの下位互換性を維持するために 2 つの例外があります。

  • 例外処理。 文字列リテラルがスローされた場合は文字列リテラルとしてキャッチされます。

  • テンプレートの推論。 文字列リテラルがテンプレート引数として渡された場合は String に変換されません。 ただし、汎用引数として渡された文字列リテラルは String に昇格されます。

また、コンパイラには 3 種類の演算子のサポートが組み込まれています。これらの演算子は、オーバーライドして動作をカスタマイズすることができます。

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

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

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

コンパイラに String が渡されると、必要に応じてボックス化されてから、オブジェクトが文字列と連結されます (ToString を使用)。

/clr:oldSyntax を指定してコンパイルする場合、文字列リテラルは String に変換されません。

注意

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

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

要件

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

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

// 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 (共通言語ランタイムのコンパイル)

概念

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