Powercoder
Powercoder

Reputation: 755

C++/CLR - overload method inside interface class

Is there way to assign many signatures of interface method to one implementation?

For example, anything ITransformable can be moved by vector or triple of coordinates:

public interface class ITransformable
{
public:
    void move(double x, double y, double z);
    void move(Vector ^ offset);
};

Such notation obligues programmer to implement both methods in each child class, but only one of them will have useful body and other will just refer to the first like

public ref class Thing : public ITransformable
{
    virtual void move(double x, double y, double z)
    {
        //Each child implements it in it's own way
        ...
    }
    virtual void move(Vector ^ offset)
    {
        //It is the same for all childs, copy it and paste everywhere
        move(offset->x, offset->y, offset->z);
    }
}

Is there something in the kind:

public interface class ITransformable
{
public:
    //Implement me
    void move(double x, double y, double z);

    //Need no overriding anymore, just use implementation of the method above
    void move(Vector ^ offset) : move(offset->x, offset->y, offset->z);
};

without multi inheritance (let's say Thing has inherited some non-interface class, so ITransformable can't be abstract class).

Upvotes: 0

Views: 76

Answers (1)

David Yaw
David Yaw

Reputation: 27864

There's no way to do what you're looking for. Interfaces are pure, never containing any implementations.

You could make the case that the two methods are redundant, and providing just one is sufficient.

The closest you'd be able to come to what you're looking for would be if both of the overloads took a single parameter, and the parameter could be converted. For example, define the method to take VectorFloat, and calling it with VectorInt would call the conversion.

Upvotes: 2

Related Questions