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
アセンブリの外部のインターフェイスのアクセシビリティ。 設定可能な値は public および private です。 private が既定値です。 入れ子になったインターフェイスに指定子を interface_access 指定することはできません。

name
インターフェイスの名前。

inherit_access
のアクセシビリティ base_interface。 基底インターフェイスで許可されるアクセシビリティは public (既定値) だけです。

base_interface
(省略可能)インターフェイスの基本インターフェイス name

解説

interface structinterface class と等価です。

インターフェイスには、関数、イベント、およびプロパティの宣言を含めることができます。 すべてのインターフェイス メンバーには、パブリック アクセシビリティがあります。 インターフェイスには静的データ メンバー、関数、イベント、およびプロパティを含めることもでき、これらの静的メンバーはインターフェイスに定義する必要があります。

インターフェイスは、クラスを実装できる方法を定義します。 インターフェイスはクラスではなく、クラスはインターフェイスのみを実装できます。 インターフェイスに宣言される関数をクラスで定義すると、オーバーライドなしで関数が実装されます。 そのため、名前の参照にはインターフェイス メンバーは含まれません。

インターフェイス class から派生した A または struct インターフェイスのすべてのメンバーを実装する必要があります。 インターフェイス nameを実装する場合は、一覧のインターフェイスも実装する base_interface 必要があります。

詳細については、以下を参照してください:

他の CLR 型については、クラスと構造体に関する記事を参照してください。

コンパイル時に、型が __is_interface_class(type) のインターフェイスかどうかを検出できます。 詳細については、型の特性に対するコンパイラのサポートを参照してください

開発環境では、キーワード (keyword)を強調表示して (たとえば) F1 キーを押すと、interface classこれらのキーワード (keyword)に関する F1 ヘルプを取得できます。

Windows ランタイム

解説

(この言語機能には Windows ランタイムのみに適用される特記事項がありません。)

必要条件

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

共通言語ランタイム

解説

(この言語機能には共通言語ランタイムのみに適用される特記事項がありません。)

必要条件

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

次のコード例では、インターフェイスで clock 関数の動作を定義する方法を示します。

// 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

次のコード例では、同じシグネチャが宣言された複数のインターフェイスと、これらのインターフェイスがクラスによって使用される場所を指定して関数を実装する 2 つの方法を示します。

// 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 でのコンポーネント拡張