Is there any principle about C++ method overriding?

Is there any principle about method overriding?

I’m already accustomed to use Super::BeginPlay, Super::Tick etc…
But, I want to know which method should call Super:: keyword or not.

Is there any unreal’s overriding principles? or any document about that?

The general rule is:

If the definition of the function in the base class is pure – that is, declared with = 0 – they it typically doesn’t have a definition, and thus shouldn’t be called with Super.

If the definition of the function in the base class is concrete – that is, it has a definition – then you should call Super.

If you want to do anything fancier than this, they you have to navigate into the implementation of the base class and read the code for yourself, but the above rules will generally work well.

1 Like

Thanks for answer my question!

But, What about overriding custom method which is not engine provided?
Is there any rule about Custom method overriding?

To be able to override a method it has to be marked as virtual.
If it is marked as virtual then you can redefine it in the inheriting class and add override at the end.

It’s up to you if you want to call super inside of the overridden function (if it’s custom).
If you skip super then the parent’s method will not be called and only the current implementation in the class will be executed.

If you mark the overridden method in your new as virtual as well, then further child classes can override the current implementation again.

When it comes to overriding engine code you usually want to call Super if you want the base function to stay intact and you just want to extend it. Sometimes the engine function also has initialization code for internal variables, if you skip it you may cause a crash later with an uninitialized variable or nullptr.

There is also a matter of unreals namespace equivalent (The _API part). Not all classes are exposed to overrides sometimes you need to access certain functions following the set “namespace” that is in front of the method (if whole UClass isn’t exposed)

1 Like

If it’s not an engine function, then the rule is the same as any other C++ class.

In general, implementation inheritance leads to designs that are hard to extend and hard to debug, because of exactly these kinds of questions. Unreal Engine kinda doubled down on that, and we have to live with it, but a composition/delegation based design, where all virtual base classes are pure abstract and all implementation classes are final, will typically lead to easier-to-work-with code, where debugging is easier, understanding context is easier, and refactoring is easier.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.