interface class (C++/CLI 및 C++/CX)

인터페이스를 선언합니다. 네이티브 인터페이스에 대한 자세한 내용은 다음을 참조하세요 __interface.

모든 런타임

구문

interface_access interface class name : inherit_access base_interface {};
interface_access interface struct name : inherit_access base_interface {};

매개 변수

interface_access
어셈블리 외부에서 인터페이스의 접근성입니다. 가능한 값은 publicprivate입니다. 기본값은 private입니다. 중첩된 인터페이스에는 지정자가 interface_access 있을 수 없습니다.

name
인터페이스의 이름입니다.

inherit_access
base_interface접근성입니다. 기본 인터페이스에 대해 허용되는 유일한 접근성은 (기본값)입니다 public .

base_interface
(선택 사항) 인터페이스 name에 대한 기본 인터페이스입니다.

설명

interface structinterface class와 같습니다.

인터페이스에는 함수, 이벤트 및 속성에 대한 선언을 포함할 수 있습니다. 모든 인터페이스 멤버는 public 접근성을 갖습니다. 인터페이스에 정적 데이터 멤버, 함수, 이벤트 및 속성을 포함할 수도 있으며, 이러한 정적 멤버는 인터페이스에서 정의해야 합니다.

인터페이스는 클래스를 구현할 수 있는 방법을 정의합니다. 인터페이스는 클래스가 아니며 클래스는 인터페이스만 구현할 수 있습니다. 클래스가 인터페이스에서 선언된 함수를 정의하는 경우 함수가 재정의되지 않고 구현됩니다. 따라서 이름 조회에는 인터페이스 멤버가 포함되지 않습니다.

A class 또는 struct 인터페이스에서 파생 된 인터페이스의 모든 멤버를 구현 해야 합니다. 인터페이스 name를 구현할 때 목록의 인터페이스 base_interface 도 구현해야 합니다.

자세한 내용은 다음을 참조하세요.

다른 CLR 형식에 대한 자세한 내용은 클래스 및 구조체를 참조하세요.

컴파일 시간에 __is_interface_class(type)을 사용하여 형식이 인터페이스인지 여부를 검색할 수 있습니다. 자세한 내용은 형식 특성에 대한 컴파일러 지원을 참조 하세요.

개발 환경에서는 키워드(keyword) 강조 표시(예interface class: F1)를 눌러 이러한 키워드(keyword) F1 도움말을 얻을 수 있습니다.

Windows 런타임

설명

(이 언어 기능에는 Windows 런타임에만 적용되는 설명이 없습니다.)

요구 사항

컴파일러 옵션: /ZW

공용 언어 런타임

설명

(이 언어 기능에는 공용 언어 런타임에만 적용되는 설명이 없습니다.)

요구 사항

컴파일러 옵션: /clr

예제

다음 코드 예제에서는 인터페이스를 통해 클록 함수의 동작을 정의할 수 있는 방법을 보여 줍니다.

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

public delegate void ClickEventHandler(int, double);

// define interface with nested interface
public interface class Interface_A {
   void Function_1();

   interface class Interface_Nested_A {
      void Function_2();
   };
};

// interface with a base interface
public interface class Interface_B : Interface_A {
   property int Property_Block;
   event ClickEventHandler^ OnClick;
   static void Function_3() { Console::WriteLine("in Function_3"); }
};

// implement nested interface
public ref class MyClass : public Interface_A::Interface_Nested_A {
public:
   virtual void Function_2() { Console::WriteLine("in Function_2"); }
};

// implement interface and base interface
public ref class MyClass2 : public Interface_B {
private:
   int MyInt;

public:
   // implement non-static function
   virtual void Function_1() { Console::WriteLine("in Function_1"); }

   // implement property
   property int Property_Block {
      virtual int get() { return MyInt; }
      virtual void set(int value) { MyInt = value; }
   }
   // implement event
   virtual event ClickEventHandler^ OnClick;

   void FireEvents() {
      OnClick(7, 3.14159);
   }
};

// class that defines method called when event occurs
ref class EventReceiver {
public:
   void OnMyClick(int i, double d) {
      Console::WriteLine("OnClick: {0}, {1}", i, d);
   }
};

int main() {
   // call static function in an interface
   Interface_B::Function_3();

   // instantiate class that implements nested interface
   MyClass ^ x = gcnew MyClass;
   x->Function_2();

   // instantiate class that implements interface with base interface
   MyClass2 ^ y = gcnew MyClass2;
   y->Function_1();
   y->Property_Block = 8;
   Console::WriteLine(y->Property_Block);

   EventReceiver^ MyEventReceiver = gcnew EventReceiver();

   // hook handler to event
   y->OnClick += gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);

   // invoke events
   y->FireEvents();

   // unhook handler to event
   y->OnClick -= gcnew ClickEventHandler(MyEventReceiver, &EventReceiver::OnMyClick);

   // call implemented function via interface handle
   Interface_A^ hi = gcnew MyClass2();
   hi->Function_1();
}
in Function_3

in Function_2

in Function_1

8

OnClick: 7, 3.14159

in Function_1

다음 코드 샘플에서는 여러 인터페이스에서 동일한 시그니처를 선언하고 해당 인터페이스를 클래스에서 사용하는 함수를 구현하는 두 가지 방법을 보여 줍니다.

// mcppv2_interface_class_2.cpp
// compile with: /clr /c
interface class I {
   void Test();
   void Test2();
};

interface class J : I {
   void Test();
   void Test2();
};

ref struct R : I, J {
   // satisfies the requirement to implement Test in both interfaces
   virtual void Test() {}

   // implement both interface functions with explicit overrides
   virtual void A() = I::Test2 {}
   virtual void B() = J::Test2 {}
};

참고 항목

.NET 및 UWP용 구성 요소 확장