インターフェイス クラス (C++ コンポーネント拡張)

インターフェイスを宣言します。ネイティブ インターフェイスの詳細については、 __interfaceを参照してください。

すべてのランタイム

構文

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

パラメーター

  • interface_access
    アセンブリの外部のユーザー インターフェイスの。有効値は Public,パブリック,public と privateです。private が既定値です。入れ子になったインターフェイスは interface_access 指定子を含むことはできません。

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

  • inherit_access
    base_interfaceのアクセシビリティ。基本インターフェイスの唯一許容されるユーザーは public (既定)です。

  • base_interface (省略可能)
    インターフェイス 名前の基本インターフェイス。

解説

interface structinterface classと同じです。

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

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

インターフェイスから派生したクラスまたは構造体は、インターフェイスのすべてのメンバーを実装する必要があります。インターフェイス名を実装する場合は、一覧 base_interface のインターフェイスを実装します。

詳細については、次のトピックを参照してください。

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

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

開発環境では、キーワードをinterface class(、など)を強調表示し、 F1 キーを押すと、これらのキーワードから F1 ヘルプを取得できます。

Windows ランタイム

解説

(実行時ウィンドウにのみ適用されるこの言語機能の解説はありません)。

737cydt1.collapse_all(ja-jp,VS.110).gif要件

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

共通言語ランタイム

解説

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

737cydt1.collapse_all(ja-jp,VS.110).gif要件

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

737cydt1.collapse_all(ja-jp,VS.110).gif

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

// 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();
}

出力

  
  
  
  
  
  

次の例では、これらのインターフェイスをクラスによって使用される場所で複数のインターフェイスで宣言されている同じシグネチャを持つ関数を実行する 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 {}
};

参照

概念

ランタイム プラットフォームのコンポーネントの拡張機能