Daily Unreal Column #49 - Pure virtual functions
Pure virtual functions isn't really a new concept for C++. However, Unreal Engine disallow to use them in UObject based classes. Instead, they provide their own way to mark a function as pure virtual.
In pure C++, marking a function as pure virtual is as simply as adding “= 0;” at the end of it’s declaration. For example:
class Foo
{
public:
virtual void bar() = 0;
};
Doing this inside UObject
based class though will result in a compilation error.
Instead, Epic provides a macro, PURE_VIRTUAL
. It must be added at the end of a function declaration. It takes two parameters, first one, optional, is the text displayed as fatal error when this function is not overriden by a child class. The second one is the optional return value. It’s optional, due to the fact that void functions do not return any value.
For a void function that doesn’t display text, a declaration looks as following:
virtual void Foo(int32 SomeParam) PURE_VIRTUAL(,);
For a function that returns a value, a declaration can look like this:
virtual int32 Foo(int32 SomeParam) PURE_VIRTUAL(, return INDEX_NONE;);
Finally for a function that returns a value and displays a text when printing fatal error:
virtual int32 Foo(int32 SomeParam) PURE_VIRTUAL(ClassName::Foo, return INDEX_NONE;);