Anteara
Anteara

Reputation: 759

Not exactly sure how to use interfaces for multiple inheritance c#

I'm currently in the process of changing my game engine's language from c++ to c#. In c++, I can simply inheret two classes in my class which makes things a lot simpler, however I find that this is impossible in c#. Instead, I must use interfaces.

I've looked around for examples and I know there are a lot here; I can't figure out how I could implement it in my case.

Please note that I followed a tutorial to generate this code, and as such my knowledge on polymorphism might be wrong.

The C++ code:

class TileMap : public sf::Drawable, public sf::Transformable
{
    ...
private:

    //this virtual function is simply so we don't have to do window.draw(target, states), we can just do window.draw(instance)
    //this is called polymorphism?
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the transform
        //this isn't our method, i assume it's something in draw() by default.
        //or this generates the finished quads in one image instead of multiple ones.
        states.transform *= getTransform();

        // apply the tileset texture
        //this puts the texture on to what we're going to draw (which is converted in to a single texture)
        states.texture = &m_tileset;

        // draw the vertex array
        target.draw(m_vertices, states);
    }
}

My tilemap class inherets the Drawable class. the states.transform *= getTransform() means I need to inheret the Transformable class.

However, I can't do this in c# exactly like c++, inheriting both classes doesn't work. This is where, I think, I need to use interfaces.

public interface Transformable{ }
public interface Drawable : Transformable{ }

I guess in the Drawable class I would implement the virtual draw function, however, I'm not actually implementing the getTransform function from Transformable, so I don't know how to access that like this.

Can someone please show me how I could use interfaces to do this with the function I supplied here?

Thanks.

Upvotes: 0

Views: 225

Answers (2)

supercat
supercat

Reputation: 81247

Having X inherit from Y in .NET accomplishes two largely orthogonal things:

  1. Code *inside* `X` can use `protected` instance members of the parent *object instance*, or static `protected `members of `Y`, as though they were their own.
  2. Objects of type `X` may be passed to any code which expects an instance of type `Y`.

It is only possible for an object to have one parent object instance; consequently, an object may only use protected instance members received from a single parent class (the members may be declared in a "grandparent" class, but still received through the parent). Since inheriting from a class automatically does both of the above, this means that classes may only inherit from one class. There is, however, no inherent restriction to the number of types for which an object may allow itself to be substituted. Interface implementation allows objects of any type which implements an interface to be passed to any code which expects a reference of the interface type.

Upvotes: 0

Daniel Daranas
Daniel Daranas

Reputation: 22634

Interfaces are not a substitute for inheritance.

With interfaces you just "inherit", well... an interface. That is, a bunch of signatures of public members (methods, properties). You can't actually inherit any meaningful substance from an interface. When you choose to implement an interface, you put a burden to yourself that you are going to implement all the members of the interface. It can help you in the design stage, but it will not help you reuse implementation which already exists in another class.

It is a fact that you can inherit from multiple classes in C++, and it is another fact that you can implement multiple interfaces in C#. But the latter is not the C# way of getting the former. They are two different properties, both true, one of the C++ language and the other of the C# language and the .NET platform altogether.

Upvotes: 2

Related Questions