## Reference

UE4 – Declaring and using interfaces in C++

## In A Word 简而言之

When we declarate a interface, we need to declarate two C++ classes.

• One extends from UInterface. It should be a empty class, named with prefix ‘U’.
• Another one can extend from nothing. It should declarate some functions you need, and it should be named with prefix ‘I’.

• 一个继承于UInterface，是个空类，以U前缀命名
• 另一个不用继承于什么，有实际的内容，以I前缀命名

Then you can implement the interface. Your class should extend from the one that really declarte functions, not the empty one.

We first declarate the prototype function(note that the prototype function doesn’t need to be implemented). Then declarate and implement the one named with postfix Implementation.

How to use the interface?

### Storing a reference to the interface 使用引用

Let’s say we want to have a IBroadcastListener as a property of a class, so that a user can bind a listener of its choice. This is how it will be defined in the UCLASS:

### Calling a method of the interface 调用方法

The key point here is in the call to the OnBroadcastReceived method: we need to make a static call on a function of interface named Execute_NameOfTheInterfaceMethod. The first parameter of this function is the object we want to call the function on, and thereafter we have all the parameters we want to pass to this function.

Buy Me A Coffee / 捐一杯咖啡的钱

0%
//