다음을 통해 공유


배열 공 분산

직접 또는 간접 기본 클래스 B와 D 참조 클래스가 지정 되 면 D 형식의 배열 형식 b의 배열 변수에 할당할 수 있습니다.

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

int main() {
   // String derives from Object
   array<Object^>^ oa = gcnew array<String^>(20);
}

설명

배열 요소는 할당 할당 호환 되어야 합니다. 동적 형식의 배열입니다. 배열 요소에 할당 호환 되지 않는 형식으로 throw 될 수 있는 System::ArrayTypeMismatchException 발생 합니다.

배열 공분산은 값 클래스 형식의 배열에 적용 되지 않습니다. 예를 들어, int32 배열 개체를 변환할 수 없습니다 ^ boxing 통해도 배열입니다.

예제

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

ref struct Base { int i; };
ref struct Derived  : Base {};
ref struct Derived2 : Base {};
ref struct Derived3 : Derived {};
ref struct Other { short s; };

int main() {
   // Derived* d[] = new Derived*[100];
   array<Derived^> ^ d = gcnew array<Derived^>(100);

   // ok by array covariance
   array<Base ^> ^  b = d;

   // invalid
   // b[0] = new Other;

   // error (runtime exception)
   // b[1] = gcnew Derived2;

   // error (runtime exception),
   // must be "at least" a Derived.
   // b[0] = gcnew Base;

   b[1] = gcnew Derived;
   b[0] = gcnew Derived3;
}

참고 항목

참조

배열(C++ 구성 요소 확장)