如何在托管C ++ / CLI中定义接口

时间:2022-05-09 05:01:55

Can we define interfaces in C++ using Visual Studio?

我们可以使用Visual Studio在C ++中定义接口吗?

If yes, what would be an example of defining interfaces in C++?

如果是,那么在C ++中定义接口的例子是什么?

2 个解决方案

#1


In managed C++, this is the syntax for a managed interface.

在托管C ++中,这是托管接口的语法。

using namespace System;

interface class IFoo
{
    String^ GetName();
};

#2


C++ does not have a concept of "interface" per se. They are usually emulated with abstract classes with pure virtual functions. Moreover, classes are usually substituted with structs, since default access modifier for those is public. Hence,

C ++本身没有“接口”的概念。它们通常使用具有纯虚函数的抽象类进行模拟。此外,类通常用结构替换,因为那些类的默认访问修饰符是公共的。因此,

struct IFoo
{
    virtual void Bar() = 0;
}

Also, see this.

另外,看到这个。

#1


In managed C++, this is the syntax for a managed interface.

在托管C ++中,这是托管接口的语法。

using namespace System;

interface class IFoo
{
    String^ GetName();
};

#2


C++ does not have a concept of "interface" per se. They are usually emulated with abstract classes with pure virtual functions. Moreover, classes are usually substituted with structs, since default access modifier for those is public. Hence,

C ++本身没有“接口”的概念。它们通常使用具有纯虚函数的抽象类进行模拟。此外,类通常用结构替换,因为那些类的默认访问修饰符是公共的。因此,

struct IFoo
{
    virtual void Bar() = 0;
}

Also, see this.

另外,看到这个。