c# - 仅返回类型不同的 C++ 多个接口(interface)?

标签 c# c++ templates interface overloading

做一个以人类可读的方式将 .NET IL 翻译成 C++ 的实验。

这里是问题所在:C# 允许您解析具有相同方法名称但返回类型不同的多个接口(interface)。 C++ 似乎不支持这一点,但是使用 vTable 无法解析两个接口(interface)(或者我错了吗?)。

我找到了一种使用模板在 C++ 中复制 C# 方法的方法,但想知道是否有一种方法不需要模板来解决相同的问题?模板很冗长,我如果可能,我不希望将它们用于每种接口(interface)类型。

这是 C++ 版本。

template<typename T>
class IMyInterface
{
    public: short (T::*Foo_IMyInterface)() = 0;
};

template<typename T>
class IMyInterface2
{
    public: int (T::*Foo_IMyInterface2)() = 0;
};

class MyClass : public IMyInterface<MyClass>, public IMyInterface2<MyClass>
{
    public: MyClass()
    {
        Foo_IMyInterface = &MyClass::Foo;
        Foo_IMyInterface2 = &MyClass::IMyInterface2_Foo;
    }

    public: virtual short Foo()
    {
        return 1;
    }

    private: int IMyInterface2_Foo()
    {
        return 1;
    }
};

class MyClass2 : public MyClass
{
    public: virtual short Foo() override
    {
        return 2;
    }
};

void InvokeFoo(IMyInterface<MyClass>* k)
{
    (((MyClass*)k)->*k->Foo_IMyInterface)();
}

void main()
{
    auto a = new MyClass2();
    InvokeFoo(a);
}

这是 C++ 所基于的 C# 引用源。

interface IMyInterface
{
    short Foo();
}

interface IMyInterface2
{
    int Foo();
}

class MyClass : IMyInterface, IMyInterface2
{
    public virtual short Foo()
    {
        return 1;
    }

    int IMyInterface2.Foo()
    {
        return 1;
    }
}

class MyClass2 : MyClass
{
    public override short Foo()
    {
        return 2;
    }
}

namespace CSTest
{
    class Program
    {
        static void InvokeFoo(IMyInterface k)
        {
            k.Foo();
        }

        static void Main(string[] args)
        {
            var a = new MyClass2();
            InvokeFoo(a);
        }
    }
}

此 C++ 方法在下面不起作用,但希望它能起作用(它更符合我的要求)。

class IMyInterface
{
    public: virtual short Foo() = 0;
};

class IMyInterface2
{
    public: virtual int Foo() = 0;
};

class MyClass : public IMyInterface, public IMyInterface2
{
    public: virtual short Foo()
    {
        return 1;
    }

    private: int IMyInterface2::Foo()// compiler error
    {
        return 1;
    }
};

class MyClass2 : public MyClass
{
    public: virtual short Foo() override
    {
        return 2;
    }
};

void InvokeFoo(IMyInterface* k)
{
    k->Foo();
}

void main()
{
    auto a = new MyClass2();
    InvokeFoo(a);
}

最佳答案

问题是您不能仅根据返回类型进行重载。

最后一个 stackoverflow 线程指出使用运算符可以实现重载。

struct func {
    operator string() { return "1"; }
    operator int() { return 2; }
};

int main() {
    int x = func(); // calls int version
    string y = func(); // calls string version
    double d = func(); // calls int version
    cout << func() << endl; // calls int version
    func(); // calls neither
}

不过你不能说出它们的名字,这很快就会变成一团糟。

参数列表必须更改。 Victor Padureau 建议使用 void 返回类型并将值的类型作为引用传递给方法中的值,这将起作用。您还可以更改不同类型的方法名称。

class my_interface
{
public: 
    virtual short foo_short() = 0;
};

class my_interface2
{
public: 
    virtual int foo_int() = 0;
};

class my_class : public my_interface, public my_interface2
{
public: 
    short foo_short() override
    {
        return 1;
    }

    int foo_int() override
    {
        return 1;
    }
};

class my_class2 : public my_class
{
public: 
    virtual short foo_short() override
    {
        return 2;
    }
};

void InvokeFoo(my_interface* k)
{
    short result = k->foo_short();
    std::cout << result << std::endl;
}

void main()
{
    auto a = new my_class2();
    InvokeFoo(a);
}

关于c# - 仅返回类型不同的 C++ 多个接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54194130/

相关文章:

c# - 如何使用 LINQ 和 C# 插入记录并返回该记录的主键

c# - Linq InsertAllOnSubmit 如果不存在

c# - 快速检索文件夹和所有子文件夹中的文件名列表

c++ - 为什么原始 curl 构造函数 {} 不返回右值?

c++ - doxygen 文档 C++ 类模板

c++ - 如何在 C++ 中生成任意嵌套的 vector ?

c# - 使用微型 ORM 时的最佳策略?

c++ - 使用 var_arg 为函数调用传递参数

c++ - 在编译期间操作字符串的 C/C++ 宏

c++ - 什么是函数的左值引用?