如何在 VC++/CLI 中实现 VB.net 中定义的接口

How to implement an interface defined in VB.net in VC++/CLI?

本文关键字:net VB 定义 接口 实现 VC++ CLI      更新时间:2023-10-16

我有一个基于 VB.net 的界面,如下所示:

Namespace Foo
    Public Interface Bar 
        ReadOnly Property Quuxes as Quux()
    End Interface
End Namespace

我现在想在 VC++/CLI 中实现它(因为我需要从非托管的第三方 DLL 接口函数),但是我无法弄清楚如何实现它的正确语法。

这是我到目前为止拥有的头文件的相关部分:

namespace Foo {
    public ref class ThirdPartyInterfacingBar : Bar {
    public:
        ThirdPartyInterfacingBar();
        virtual property array<Quux^, 1>^ Quuxes;
    };
}

但现在我被困在如何在随附的.cpp文件中实现这一点。

当做类似的事情时(#include剥离)

namespace Foo{
    array<Quux^, 1>^ ThirdPartyInterfacingBar::Quuxes { /*...*/ }
}

我得到:C2048: function 'cli::array<Type,dimension> ^Foo::ThirdPartyInterfacingBar::Quuxes::get(void)' already has a body

我唯一能想到的就是这样的事情:

namespace Foo {
    public ref class ThirdPartyInterfacingBar : Bar {
    private:
        array<Quux^, 1>^ delegateGetQuuxes();
    public:
        ThirdPartyInterfacingBar();
        virtual property array<Quux^, 1>^ Quuxes {
            array<Quux^, 1>^ get() {
                return delegateGetQuuxes();
            }
        }
    };
}

并在随附的 cpp 文件中实现delegateGetQuuxes。但我认为这很丑陋,因为我不想在标题中有任何逻辑。有没有更好的方法?

看起来你只是忘记了get()。 正确的语法是:

.h file:
public ref class ThirdPartyInterfacingBar : Bar {
public:
    property array<Quux^>^ Quuxes {
        virtual array<Quux^>^ get();
    }
};
.cpp file:
array<Quux^>^ ThirdPartyInterfacingBar::Quuxes::get() {
    return delegateGetQuuxes();
}