방법: C++/CLI에서 속성 사용

이 문서에서는 C + + 속성을 사용 하는 방법을 보여 줍니다. + CLI.

기본 속성

기본 속성에 대 한-단순히 할당 및 전용 데이터 멤버를 검색, 컴파일러가 방금 속성의 데이터 형식을 지정 하는 경우 자동으로 제공 하기 때문에 set 접근자 함수 및 명시적으로 get을 정의할 필요가 없습니다.이 코드는 기본 속성을 보여 줍니다.

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

ref class C {
public:
   property int Size;
};

int main() {
   C^ c = gcnew C;
   c->Size = 111;
   Console::WriteLine("c->Size = {0}", c->Size);
}

Output

  

정적 속성

이 코드 예제에서는 선언 하 고 정적 속성을 사용 하는 방법을 보여 줍니다.정적 속성은 해당 클래스의 정적 멤버를 액세스할 수 있습니다.

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

ref class StaticProperties {
   static int MyInt;
   static int MyInt2;

public:
   static property int Static_Data_Member_Property;

   static property int Static_Block_Property {
      int get() {
         return MyInt;
      }

      void set(int value) {
         MyInt = value;
      }      
   }
};

int main() {
   StaticProperties::Static_Data_Member_Property = 96;
   Console::WriteLine(StaticProperties::Static_Data_Member_Property);

   StaticProperties::Static_Block_Property = 47;
   Console::WriteLine(StaticProperties::Static_Block_Property);
}

Output

  

인덱싱된 속성

인덱싱된 속성은 일반적으로 아래 첨자 연산자를 사용 하 여 액세스 되는 데이터 구조를 노출 합니다.

사용 하는 경우 인덱싱된 기본 속성, 단지 클래스 이름을 참조 하 여 데이터 구조를 액세스할 수 있지만 사용자 정의 된 인덱싱된 속성을 사용 하는 경우 데이터 구조에 액세스 하는 속성 이름을 지정 해야 합니다.

기본 인덱서를 사용 하 여 액세스 하는 방법에 대 한 정보는 this 포인터를 참조 하십시오 값 형식 및 참조 형식에서 이 포인터의 의미.

C#에 기록 되는 인덱서를 사용 하는 방법에 대 한 내용은 방법: C# 인덱서 사용(C++/CLI).

이 코드 예제에서는 기본 및 사용자 정의 하는 인덱싱된 속성을 사용 하는 방법을 보여 줍니다.

// mcppv2_property_2.cpp
// compile with: /clr
using namespace System;
public ref class C {
   array<int>^ MyArr;

public:
   C() {
      MyArr = gcnew array<int>(5);
   }

   // default indexer
   property int default[int] {
      int get(int index) {
         return MyArr[index];
      }
      void set(int index, int value) {
         MyArr[index] = value;
      }
   }

   // user-defined indexer
   property int indexer1[int] {
      int get(int index) {
         return MyArr[index];
      }
      void set(int index, int value) {
         MyArr[index] = value;
      }
   }
};

int main() {
   C ^ MyC = gcnew C();

   // use the default indexer
   Console::Write("[ ");
   for (int i = 0 ; i < 5 ; i++) {
      MyC[i] = i;
      Console::Write("{0} ", MyC[i]);
   }

   Console::WriteLine("]");

   // use the user-defined indexer
   Console::Write("[ ");
   for (int i = 0 ; i < 5 ; i++) {
      MyC->indexer1[i] = i * 2;
      Console::Write("{0} ", MyC->indexer1[i]);
   }

   Console::WriteLine("]");
}

Output

  

기본 인덱서를 사용 하 여 호출 방법을 보여 주는 다음 예제는 this 포인터입니다.

// call_default_indexer_through_this_pointer.cpp
// compile with: /clr /c
value class Position {
public:
   Position(int x, int y) : position(gcnew array<int, 2>(100, 100)) {
      this->default[x, y] = 1;
   }

   property int default[int, int] {
      int get(int x, int y) {
         return position[x, y];
      }

      void set(int x, int y, int value) {}
   }

private:
   array<int, 2> ^ position;
};

이 샘플을 사용 하는 방법을 보여 줍니다. DefaultMemberAttribute 의 기본 인덱서를 지정 하려면:

// specify_default_indexer.cpp
// compile with: /LD /clr
using namespace System;
[Reflection::DefaultMember("XXX")]
public ref struct Squares {
   property Double XXX[Double] {
      Double get(Double data) {
         return data*data;
      }
   }
};

다음 샘플에서는 앞의 예제에서 생성 되는 메타 데이터를 사용 합니다.

// consume_default_indexer.cpp
// compile with: /clr
#using "specify_default_indexer.dll"
int main() {
   Squares ^ square = gcnew Squares();
   System::Console::WriteLine("{0}", square[3]);
}

Output

  

가상 속성

이 코드 예제에서는 가상 속성 선언 및 사용 하는 방법을 보여 줍니다.

// mcppv2_property_4.cpp
// compile with: /clr
using namespace System;
interface struct IEFace {
public:
   property int VirtualProperty1;
   property int VirtualProperty2 {
      int get();
      void set(int i);
   }
};

// implement virtual events
ref class PropImpl : public IEFace {
   int MyInt;
public:
   virtual property int VirtualProperty1;

   virtual property int VirtualProperty2 {
      int get() {
         return MyInt;
      }
      void set(int i) {
         MyInt = i;
      }
   }
};

int main() {
   PropImpl ^ MyPI = gcnew PropImpl();
   MyPI->VirtualProperty1 = 93;
   Console::WriteLine(MyPI->VirtualProperty1);

   MyPI->VirtualProperty2 = 43;
   Console::WriteLine(MyPI->VirtualProperty2);
}

Output

  

추상 및 봉인 속성

하지만 abstract(C++ 구성 요소 확장)sealed(C++ 구성 요소 확장) 키워드에는 ECMA C +를 올바르게 지정 + Visual C++ 컴파일러, CLI 사양 없습니다 지정이 trivial 속성에도 특수 한 속성의 속성 선언에 대.

봉인 또는 추상 속성을 선언할 수 있는 특수 한 속성을 정의 하 고 다음 지정 해야는 abstract 또는 sealed 키워드를 get 접근자 함수를 설정 합니다.

// properties_abstract_sealed.cpp
// compile with: /clr
ref struct A {
protected:
   int m_i;

public:
   A() { m_i = 87; }

   // define abstract property
   property int Prop_1 {
      virtual int get() abstract;
      virtual void set(int i) abstract;
   }
};

ref struct B : A {
private:
   int m_i;

public:
   B() { m_i = 86; }

   // implement abstract property
   property int Prop_1 {
      virtual int get() override { return m_i; }
      virtual void set(int i) override { m_i = i; }
   }
};

ref struct C {
private:
   int m_i;

public:
   C() { m_i = 87; }

   // define sealed property
   property int Prop_2 {
      virtual int get() sealed { return m_i; }
      virtual void set(int i) sealed { m_i = i; };
   }
};

int main() {
   B b1;
   // call implementation of abstract property
   System::Console::WriteLine(b1.Prop_1);

   C c1;
   // call sealed property
   System::Console::WriteLine(c1.Prop_2);
}

Output

  

다차원 속성

벗어난 수의 매개 변수를 사용 하는 속성 접근자 메서드를 정의 하려면 다차원 속성을 사용할 수 있습니다.

// mcppv2_property_5.cpp
// compile with: /clr
ref class X {
   double d;
public:
   X() : d(0) {}
   property double MultiDimProp[int, int, int] {
      double get(int, int, int) {
         return d;
      }
      void set(int i, int j, int k, double l) {
         // do something with those ints
         d = l;
      }
   }

   property double MultiDimProp2[int] {
      double get(int) {
         return d;
      }
      void set(int i, double l) {
         // do something with those ints
         d = l;
      }
   }
   
};

int main() {
   X ^ MyX = gcnew X();
   MyX->MultiDimProp[0,0,0] = 1.1;
   System::Console::WriteLine(MyX->MultiDimProp[0, 0, 0]);
}

Output

  

오버 로드할 속성 접근자

다음 예제에서는 인덱싱된 속성을 오버 로드 하는 방법을 보여 줍니다.

// mcppv2_property_6.cpp
// compile with: /clr
ref class X {
   double d;
public:
   X() : d(0.0) {}
   property double MyProp[int] {
      double get(int i) {
         return d;
      }

      double get(System::String ^ i) {
         return 2*d;
      }

      void set(int i, double l) {
         d = i * l;
      }
   }   // end MyProp definition
};

int main() {
   X ^ MyX = gcnew X();
   MyX->MyProp[2] = 1.7;
   System::Console::WriteLine(MyX->MyProp[1]);
   System::Console::WriteLine(MyX->MyProp["test"]);
}

Output

  

참고 항목

참조

속성(C++ 구성 요소 확장)