Inheritance C++/CLR-接口类内的重载方法
有没有办法将接口方法的多个签名分配给一个实现 例如,任何Inheritance C++/CLR-接口类内的重载方法,inheritance,interface,c++-cli,overloading,clr,Inheritance,Interface,C++ Cli,Overloading,Clr,有没有办法将接口方法的多个签名分配给一个实现 例如,任何ITransformable都可以通过向量或三重坐标移动: public interface class ITransformable { public: void move(double x, double y, double z); void move(Vector ^ offset); }; 这种表示法迫使程序员在每个子类中实现这两个方法,但其中只有一个具有有用的主体,而另一个仅引用第一个 public ref cl
ITransformable
都可以通过向量或三重坐标移动:
public interface class ITransformable
{
public:
void move(double x, double y, double z);
void move(Vector ^ offset);
};
这种表示法迫使程序员在每个子类中实现这两个方法,但其中只有一个具有有用的主体,而另一个仅引用第一个
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);
}
}
有这种东西吗:
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);
};
如果没有多重继承(比如说,
东西
继承了一些非接口类,所以ITransformable
不能是抽象类)。没有办法完成您想要的任务。接口是纯的,从不包含任何实现
您可以假设这两种方法是冗余的,只提供一种就足够了
如果两个重载都使用一个参数,并且该参数可以转换,则最接近您要查找的内容。例如,定义采用
VectorFloat
的方法,使用VectorInt
调用该方法将调用转换 没有办法做你想做的事。接口是纯的,从不包含任何实现
您可以假设这两种方法是冗余的,只提供一种就足够了
如果两个重载都使用一个参数,并且该参数可以转换,则最接近您要查找的内容。例如,定义采用
VectorFloat
的方法,使用VectorInt
调用该方法将调用转换 这是不可能的,只有抽象基类才能做到这一点。实现接口的类中的一个线性方法不会使您的速度慢很多。这是不可能的,只有抽象基类才能做到这一点。类中实现接口的一行程序方法不会让您慢很多。正如您所说,没有答案,所以我不会接受,但请接受我的意见。您说没有答案,所以我不会接受,但请接受我的意见