Confused? You're not alone

It took me a long time to make sense of these two videos (“Interfaces to Invert Dependencies” and “Solution: Injecting Dependencies”).

Before you start watching them, I suggest you:

  1. acquire some general knowledge about interfaces in programming
  2. acquire some general knowledge about interfaces in c++
  3. learn about c++ interfaces in UnrealEngine
  1. learn about Dependency Inversion
  • in this context, Dependency Inversion calls for UYourGameInstance and UYourMainMenu to have includes of UMainMenuInterface and NOT of each other

Hi,
Just to be clear. This is considered an intermediate level course. This means what is expected of students is a moderate understanding of Unreal and a solid understanding of C++ and Object Oriented programming. I usually recommend around 2+ years of industry experience in C++.

If you have that experience, the suggested subjects should already be thoroughly understood by any developer.

As to your point 2, C++ does not have interfaces - that’s a C# thing (possibly Java too) and an “interface” is just a pure abstract class i.e. a class with no implementation, just definition.

The label is perhaps misleading in the section but really it is the abstraction of the multiplayer functionality. Why the menu reference is there in the game instance is the simple need to be able to create it - it has to be created somewhere. That can’t be done in the “interface” as it has no functionality. The menu cannot be created by itself so the only other way is to include the header so it can be created. There may be the possibility of abstracting further but then it gets more complicated.

Well, what else can we call this idea of using multiple inheritance in conjunction with a pure abstract class in c++ to imitate “true interfaces”?

Edit: added “pure” and reworded sentence a little bit

Multiple Inheritance is just what the name says and not the same as a language that utilises interfaces and uses single inheritance. Where a language like C# can inherit, the model is not multiple inheritance like C++. With C# as an example, you can inherit from a single class, abstract or fully implemented. You can also implement as many interfaces as you want but this is where an interface is different from an abstract class. Interfaces only enforce what must be implemented. Abstract classes can have implementation.

A Pure Abstract class is the closest you can get to an interface in C++ but a pure abstract can also have data defined for use when inherited. A Pure Abstract class in C# for example doesn’t mean you can also inherit from another class - you cannot. So, with a single inheritance model like this, having an interface type for enforcing implementation requirements is important. They behave more like a contract really, enforcing the defined requirements at compile time.

So, Interfaces are not the same as Pure Abstracts from that point of view.

Back to multiple inheritance - C++ supports inheritance from multiple classes and they do not have to be pure abstracts or abstracts even. In fact, often this has a major advantage as you can have an abstract with some common functionality but no need for a pure abstract class above it like you would often do with C# Interfaces.

You might argue it is semantics, but it is an incredibly important distinction to be aware of.

In addition to this, C++ has one other major key difference over languages like C# or Java and that it is a multi-paradigm language. It is possible and in fact completely valid to create an application without any classes or use of classes at all - procedural implementation.

So, it is important to be aware of this. I hope this clarifies why interfaces and abstracts are not the same thing, and why C++ doesn’t actually have interfaces (in that they aren’t required due to multiple inheritence)

it’s only yesterday that I understood what Interfaces do, by God’s miracle:

To put it into extremely simple terms, Interfaces allow you to split functions inside a class to different C# (or C++ for you Unreal users) Script files. In other words, instead of writing one HUGE function in a class that would not only be incredibly hard to read and lengthy, and naturally… hard to edit when needed (think of a saving system that would have to deal with health, player position, inventory, equipment, etc…), it would also be calling on tons of namespaces. You simply write the functions you want to adjust in multiple files into one class, and every time you want to tune them, you call that class through Inheritance, and code it accordingly in each file (which is, naturally, much easier to read).

What makes this amazing is that you’re dealing directly with what you want to tune, rather than with EVERYTHING you have ever coded in one big ugly block

That would be partial classes where you can declare different functions in different files. An interface is something completely different - that is where you declare a selection of methods that need to be implemented when the class declaration specifies the interface.

e.g.

interface foo {
   void fn1(string instr);
   string fn2();
}

class  foo_impl: foo {
   void fn1(string instr){...}
   string fn2() {return "test"}
}

should seem fixed now, right? Apologies for any mistakes, the concept is still brand new in my head, and one of my ways of solidifying knowledge is by trying to explain it to others :sweat_smile:

2 Likes

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

Privacy & Terms