I’m doing something where I “feel” like I need a class with multiple inheritance, but this isn’t allowed. I looked at interfaces but they do not allow for default implementation.
I have read and I am not seeing a way to do something similar to multiple inheritance or an interface with default implementation? Am I missing something? Anybody have any suggestions or keywords to search? Thanks.
There’s no multiple inheritance in c#, but there are various ways you can provide something similar.
Imagine desired state is: class C: A,B
Typical pattern is to use an interface which wraps B and define C as: class C: A, IB
with the B implemetation in provided as:
private B b;
public void MethodFromB() { b.MethodFromB()) }
An alternative is to use static methods/class extensions if the methods from B can be reasonably considered utility classes (or just suitable to having a static implementation).
Another alternative is to embrace component approach of Unity. A lot of people shy away from this for perceived performance issues (hint: rarely an issue) but for some implementation this is a really natural fit.
If you explain what you are trying to do in more detail, we might have some more suggestions. There are good reasons multiple inheritance is not allowed and there are usually simple ways to accomplish what you want without it.
Without more details, it’s hard to make suggestions, but in Unity you’re probably better off using composition, or interfaces with a base class for each interface.
The monster has several feats such as melee attack, jump, faceplant, thrash, walk.
My class structure
Class Creature
Class Monster : Creature
Class Hero : Creature
I have abilities/feats in the game that creatures (both monsters and hero) can use. For example
Class Feat
Class FeatMeleeAttack : Feat
Class FeatCastFireball : Feat
Most feats, such as FeatMeleeAttack will mostly be the same for the hero and monster (they’ll plan animation, have internal timers, etc.). But there will be slight differences. All hero feats will be activated by keybinding, have network communication between client & server, have an GUI action bar icon, etc. All monster feats will be slightly different as well, they will be controlled by monster AI (a state machine). Other than that all the guts of the feat are the same.
So I’d like to do something like this
IFeatHero - would contain things that all hero feats have like keybinding and GUI icon
IFeatMonster - would contain things that all monster feats have that relate to monster AI state machine
And then…
FeatMeleeAttackHero : FeatMeleeAttack, IFeatHero
FeatMeleeAttackMonster : FeatMeleeAttack, IFeatMonster
But multiple inheritance isn’t allowed… and interfaces don’t have default implementation or allow for fields. I’m not sure the proper way to handle this.
You need to break things up instead of creating classes that do everything.
Simple example with a hero and attacks. Have an attack class that defines things specific to an attack. What it does, how much damage it can do, etc… The Hero class could then have a list of attacks, maybe just a list of attack instances of various types. Attack might have an enum that specifies the type of attack it is.
Then you have a combat class that handles the combat. It might have an attack method that takes a source, target, and the attacks used.
And so on and so on for all of your functionality.
It’s going to seem wrong coming from how you are used to doing things, but break stuff up into parts where each part just does one thing and one thing only. Do this for classes and methods. It’s a great way to force yourself to think in abstractions, which is the hardest thing to do well, and what programming is really all about.
So instead of inheritance just keep breaking out common functionality into their own classes. When you see duplication of code break that out into it’s own class or method.
For now just have instances of your various classes on other classes that need the functionality. There are ways to handle this elegantly but honestly getting the abstraction stuff down is so important, it dwarfs everything else. By the time you get good at thinking of how to break out code into logical pieces, the rest of it will fall into place.
You want to break this up with composition. In particular the input/AI details should be separate from the ability. This lets you use the same abilities on monsters and players. Any subtle differences should be set in the inspector.
Allowing multiple inheritence makes the rules about function overloads and virtual dispatch decidedly more tricky, as well as the language implementation around object layouts. These impact language designers/implementors quite a bit, and raise the already high bar to get a language done, stable and adopted.
It is simple to think this way, if class A inherits from multiple classes, then the class A will have the same grandparent class multiple times, this means the code will be complicated and a series of bugs will go unacknowledged. Personally, I think multiple inheritance has a bad rap, and that a well done system of trait style composition would be really powerful/useful… but there are a lot of ways that it can be implemented badly, and a lot of reasons it’s not a good idea in a language like C++.