description | title | ms.date | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | |||||
---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: interface class (C++/CLI and C++/CX) | interface class (C++/CLI and C++/CX) | 04/15/2022 | reference |
|
| 3ccea701-f50b-4da7-ad6b-f0ee1203e2b9 |
Declares an interface. For information on native interfaces, see __interface
.
interface_access interface classname : inherit_access base_interface {}; interface_access interface structname : inherit_access base_interface {};
interface_access
The accessibility of an interface outside the assembly. Possible values are public
and private
. private
is the default. Nested interfaces can't have an interface_access
specifier.
name
The name of the interface.
inherit_access
The accessibility of base_interface
. The only permitted accessibility for a base interface is public
(the default).
base_interface
(Optional) A base interface for interface name
.
interface struct
is equivalent to interface class
.
An interface can contain declarations for functions, events, and properties. All interface members have public accessibility. An interface can also contain static data members, functions, events, and properties, and these static members must be defined in the interface.
An interface defines how a class may be implemented. An interface isn't a class and classes can only implement interfaces. When a class defines a function declared in an interface, the function is implemented, not overridden. Therefore, name lookup doesn't include interface members.
A class
or struct
that derives from an interface must implement all members of the interface. When implementing interface name
, you must also implement the interfaces in the base_interface
list.
For more information, see:
For information on other CLR types, see Classes and Structs.
You can detect at compile time if a type is an interface with __is_interface_class(type)
. For more information, see Compiler support for type traits.
In the development environment, you can get F1 help on these keywords by highlighting the keyword (for example, interface class
) and pressing F1.
(There are no remarks for this language feature that apply to only the Windows Runtime.)
Compiler option: /ZW
(There are no remarks for this language feature that apply to only the common language runtime.)
Compiler option: /clr
The following code example demonstrates how an interface can define the behavior of a clock function.
// mcppv2_interface_class.cpp// compile with: /clrusingnamespaceSystem; public delegate voidClickEventHandler(int, double); // define interface with nested interface public interface classInterface_A { voidFunction_1(); interface classInterface_Nested_A { voidFunction_2(); }; }; // interface with a base interface public interface classInterface_B : Interface_A { property int Property_Block; event ClickEventHandler^ OnClick; staticvoidFunction_3() { Console::WriteLine("in Function_3"); } }; // implement nested interface public ref classMyClass : publicInterface_A::Interface_Nested_A { public:virtualvoidFunction_2() { Console::WriteLine("in Function_2"); } }; // implement interface and base interface public ref classMyClass2 : publicInterface_B { private:int MyInt; public:// implement non-static functionvirtualvoidFunction_1() { Console::WriteLine("in Function_1"); } // implement property property int Property_Block { virtualintget() { return MyInt; } virtualvoidset(int value) { MyInt = value; } } // implement eventvirtual event ClickEventHandler^ OnClick; voidFireEvents() { OnClick(7, 3.14159); } }; // class that defines method called when event occurs ref classEventReceiver { public:voidOnMyClick(int i, double d) { Console::WriteLine("OnClick: {0}, {1}", i, d); } }; intmain() { // call static function in an interfaceInterface_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
The following code sample shows two ways to implement functions with the same signature declared in multiple interfaces and where those interfaces are used by a class.
// mcppv2_interface_class_2.cpp// compile with: /clr /c interface classI { voidTest(); voidTest2(); }; interface classJ : I { voidTest(); voidTest2(); }; ref structR : I, J { // satisfies the requirement to implement Test in both interfacesvirtualvoidTest() {} // implement both interface functions with explicit overridesvirtualvoidA() = I::Test2 {} virtualvoidB() = J::Test2 {} };