Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C++/CLI中,当存在多个重载时,如何选择要重写的方法?_C#_Inheritance_C++ Cli - Fatal编程技术网

C# 在C++/CLI中,当存在多个重载时,如何选择要重写的方法?

C# 在C++/CLI中,当存在多个重载时,如何选择要重写的方法?,c#,inheritance,c++-cli,C#,Inheritance,C++ Cli,在C项目中,我有以下几种类型: public struct Struct<T> { public T Field; } public interface IInterface<T> { T F(T x); } public abstract class Class<T> : IInterface<T>, IInterface<Struct<T>> { virtual public T F(T x) { ret

在C项目中,我有以下几种类型:

public struct Struct<T> { public T Field; }
public interface IInterface<T>
{
    T F(T x);
}
public abstract class Class<T> : IInterface<T>, IInterface<Struct<T>> {
    virtual public T F(T x) { return x; }
    virtual public T F(Struct<T> x) { return x.Field; }
    Struct<T> IInterface<Struct<T>>.F(Struct<T> x) { return default(Struct<T>); }
}
不幸的是,这不起作用:

error C2553: 'int Derived::F(Struct<T>)': overriding virtual function return type
differs from 'Struct<T> IInterface<Struct<T>>::F(Struct<T>)'
with
    [
        T=int
    ]
在C++/CLI中:

public ref class B : A {
public:
    virtual long F(int x) override { return 2L; }        
    virtual int IInterface_F(int x) sealed = IInterface::F {
        return ??? // call A's version of IInterface::F(x), which returns three
    }
}

如何使用您提供的代码从IInterface\u F中调用A版本的IInterface::F?

尽管类对我来说很奇怪:

public ref class Derived sealed : public Class<int> {
public:
    virtual int F(Struct<int> x) override;
};
编译器抱怨:

错误C2553:“int-Derived::fsstruct”:重写虚拟函数返回类型与“Struct IInterface>::fsstruct”不同

with [ T=int ] with [ T=int ] 错误C3766:“派生”必须为接口方法“Struct IInterface>::fsstruct”提供实现

with [ T=int ] with [ T=int ] 错误C3612:“派生”:密封类不能有任何纯虚方法 必须定义以下方法:

'Struct IInterface>::F(Struct)' : is abstract with [ T=int ] 因此,我认为为接口方法提供一个实现,就像编译器所说的那样,这就解决了问题:

public ref class Derived sealed : public Class<int> {
public:
    virtual Struct<int> IInterface_F(Struct<int> x) 
        sealed=IInterface<Struct<int>>::F {
        return Struct<int>(); // equivalent to default(Struct<int>) in c#
    }

    virtual int F(Struct<int> x) override {
        return 0; // equivalent to default(int) in c#
    }
};

使用您提供的代码,尽管类在我看来很奇怪:

public ref class Derived sealed : public Class<int> {
public:
    virtual int F(Struct<int> x) override;
};
编译器抱怨:

错误C2553:“int-Derived::fsstruct”:重写虚拟函数返回类型与“Struct IInterface>::fsstruct”不同

with [ T=int ] with [ T=int ] 错误C3766:“派生”必须为接口方法“Struct IInterface>::fsstruct”提供实现

with [ T=int ] with [ T=int ] 错误C3612:“派生”:密封类不能有任何纯虚方法 必须定义以下方法:

'Struct IInterface>::F(Struct)' : is abstract with [ T=int ] 因此,我认为为接口方法提供一个实现,就像编译器所说的那样,这就解决了问题:

public ref class Derived sealed : public Class<int> {
public:
    virtual Struct<int> IInterface_F(Struct<int> x) 
        sealed=IInterface<Struct<int>>::F {
        return Struct<int>(); // equivalent to default(Struct<int>) in c#
    }

    virtual int F(Struct<int> x) override {
        return 0; // equivalent to default(int) in c#
    }
};

谢谢,这似乎消除了错误消息,但请参阅问题中的“我的更新”了解剩余问题。哦,如果我的代码看起来很奇怪,也不用担心:它是一个更大的整体的最小简化版本。我想保留任何可能相关的内容。谢谢,这似乎可以消除错误消息,但有关剩余问题,请参阅问题中的我的更新。哦,如果我的代码看起来很奇怪,也不用担心:它是一个更大的整体的最小简化版本。我想保留任何可能相关的内容。对于更新的问题,请尝试在c中实现类B,您会发现问题所在。@KenKin在c I中不需要重新实现接口,因此没有问题。您是否尝试过覆盖=class::F?对于更新的问题,尝试在c中实现类B,您会发现问题所在。@KenKin在c I中,不需要重新实现接口,因此没有问题。您是否尝试过override=class::F?