I search for solutions in this order: Past Code, Unreal Source, Wiki, BUF, groups.yahoo, google, screaming at monitor. – RegularX

Difference between revisions of "Interfaces"

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search
m (Redirecting to Interface)
 
(basic overview)
Line 1: Line 1:
#REDIRECT [[Interface]]
+
An [[wp:Interface (computer science)|Interface]] class is a special type of class that only defines function prototypes. Other classes implementing an interface must implement all functions defined by the interface class.
 +
 
 +
Interfaces that don't have any member declarations are called '''marker interfaces''' because they do nothing else but marking the implementing class. In [[wp:Java (programming language|Java (programming language]] this concept is used e.g. to specify whether list implementations support fast access in random order. In [[UnrealScript]], empty interfaces are not always just marker interfaces. The otherwise empty interface classes in [[UT3]] and [[UDK]] are all declared as "native" and thus contain logic or definitions declared in native C++ code.
 +
 
 +
{{expand}}
 +
 
 +
==Syntax==
 +
Interface classes start with an interface declaration:
 +
'''interface''' ''InterfaceName'' ['''extends''' ''ParentInterface''] [''modifiers'']''';'''
 +
The [[extends clause]] is optional for interface classes. If omitted, the interface extends class [[Interface]]. The Interface class itself conceptually extends class [[Object]], but should actually be seen as independent from the class tree. A [[within clause]] is not allowed for interface declarations.
 +
 
 +
===Interface modifiers===
 +
Only two modifiers can be found in [[UT3]] and [[UDK]] interface declarations:
 +
; dependson: The interface should be compiled after the specified classes or interfaces. This can be used to ensure [[struct]] and [[enum]] types used as parameter or return types in function declarations are already known to the compiler. It is only necessary if the class or interface is in the same package as this interface.
 +
; native: The interface contains additional logic or definitions in native C++ code.
 +
 
 +
===Interface members===
 +
Interfaces can declare functions, structs and enums. The functions they declare should not have a body because they cannot be called anyway. [[Variable declarations]] are not allowed in interfaces and would not make any sense, if you think about it.
 +
 
 +
==Usage==
 +
Interfaces can be implemented in any class by including the modifier <code>'''implements('''''InterfaceName''''')'''</code> in the class declaration. The compiler will make sure the class correctly implements all functions specified by the interface. Instances of that class or any of its subclasses can be assigned to variables of type ''InterfaceName''. Functions defined in the interface can be called on any object assigned to such a variable.
 +
 
 +
{{navbox unrealscript}}

Revision as of 02:15, 7 November 2009

An Interface class is a special type of class that only defines function prototypes. Other classes implementing an interface must implement all functions defined by the interface class.

Interfaces that don't have any member declarations are called marker interfaces because they do nothing else but marking the implementing class. In Java (programming language this concept is used e.g. to specify whether list implementations support fast access in random order. In UnrealScript, empty interfaces are not always just marker interfaces. The otherwise empty interface classes in UT3 and UDK are all declared as "native" and thus contain logic or definitions declared in native C++ code.


Syntax

Interface classes start with an interface declaration:

interface InterfaceName [extends ParentInterface] [modifiers];

The extends clause is optional for interface classes. If omitted, the interface extends class Interface. The Interface class itself conceptually extends class Object, but should actually be seen as independent from the class tree. A within clause is not allowed for interface declarations.

Interface modifiers

Only two modifiers can be found in UT3 and UDK interface declarations:

dependson
The interface should be compiled after the specified classes or interfaces. This can be used to ensure struct and enum types used as parameter or return types in function declarations are already known to the compiler. It is only necessary if the class or interface is in the same package as this interface.
native
The interface contains additional logic or definitions in native C++ code.

Interface members

Interfaces can declare functions, structs and enums. The functions they declare should not have a body because they cannot be called anyway. Variable declarations are not allowed in interfaces and would not make any sense, if you think about it.

Usage

Interfaces can be implemented in any class by including the modifier implements(InterfaceName) in the class declaration. The compiler will make sure the class correctly implements all functions specified by the interface. Instances of that class or any of its subclasses can be assigned to variables of type InterfaceName. Functions defined in the interface can be called on any object assigned to such a variable.