문자열(C++ 구성 요소 확장)
Visual C++ 컴파일러 지원 문자열, 텍스트 문자의 시퀀스로 나타내는 개체에 속해 있습니다.Visual C++에서 지 원하는 문자열 변수의 값은 암시적으로, 및 리터럴 값은 명시적 따옴표 붙은 문자열입니다.
모든 런타임
런타임 Windows 및 공용 언어 런타임 문자열을은 개체는 할당 된 메모리를 자동으로 관리 됩니다 나타냅니다.즉, 범위는 문자열 변수 이동 또는 응용 프로그램이 끝날 때 메모리 문자열을 명시적으로 무시 되지 않아도 됩니다.문자열 형식과 문자열 개체의 수명 자동으로 관리할 수 있는지 지정 하려면 선언에 핸들 개체 (^) 한정자입니다.
Windows 런타임
Windows 런타임 아키텍처를 Visual C++ 구현에 필요는 String 데이터 형식에 Platform 네임 스페이스입니다.또한 Visual C++ 사용자의 편의 제공는 string 데이터 형식에 속한 동의어에 대 한 Platform::String에 default 네임 스페이스입니다.
구문
// 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!";
설명
자세한 내용 및 문자열에 대 한 예제를 참조 하십시오.Platform::String, std::wstring, 및 리터럴 (플랫폼)
요구 사항
컴파일러 옵션:/ZW
공용 언어 런타임
이 항목을 사용 하 여 실행할 때 Visual C++ 컴파일러 문자열 리터럴을 처리 하는 방법에 대해 설명 합니다.의 /clr 컴파일러 옵션.사용할 /clr, 공용 언어 런타임 (CLR), C +도 사용 해야 + CLI 구문 및 개체를 관리 되는./clr에 대한 자세한 내용은 /clr(공용 언어 런타임 컴파일)를 참조하십시오.
로 컴파일하는 경우 /clr, 컴파일러 문자열 리터럴 형식의 문자열로 변환 String.유지 하려면 기존 코드는 이전 버전과 호환성은 두 가지 예외가.
예외를 처리 합니다.문자열 리터럴 throw 되 면 컴파일러는 문자열 리터럴로 catch 합니다.
템플릿 추론 합니다.템플릿 인수로 문자열 리터럴을 전달 되 면 컴파일러를 변환 하지 않습니다는 String.참고: 제네릭 인수로 전달 하는 문자열 리터럴을 승격 String.
컴파일러는 또한 기본 제공 동작을 사용자 지정 하도록 재정의할 수 있는 세 가지 연산자를 지원이 있습니다.
System::String ^ 연산자 + (System::String, System::String).
System::String ^ 연산자 + (System::Object, System::String).
System::String ^ 연산자 + (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");
}
Output
예제
다음 샘플에서는 컴파일러에서 제공 하는 연산자를 오버 로드할 수 및 컴파일러는 함수 오버 로드를 기반으로 얻을 수 표시는 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);
}
Output
예제
컴파일러 네이티브 문자열 사이 구별 하는 예제를 보여 줍니다 및 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
}
Output