Say I have a C# interface called IMyInterface
defined as follows:
假设我有一个名为IMyInterface的C#接口,定义如下:
// C# code
public interface IMyInterface
{
void Foo(string value);
string MyProperty { get; }
}
Assume I also have a C++/CLI class, MyConcreteClass
, that implements this interface and whose header is declared as follows:
假设我还有一个C ++ / CLI类MyConcreteClass,它实现了这个接口,其头部声明如下:
// C++/CLI header file
ref class MyConcreteClass : IMyInterface
{
public:
};
How does one implement the method Foo
and the property MyProperty
in the C++/CLI header?
如何在C ++ / CLI头中实现方法Foo和属性MyProperty?
My attempt results in the following compile error:
我的尝试导致以下编译错误:
error C3766: 'MyConcreteClass' must provide an implementation for the interface method 'void IMyInterface::Foo(System::String^ value)'
错误C3766:'MyConcreteClass'必须提供接口方法'void IMyInterface :: Foo(System :: String ^ value)'的实现
1 个解决方案
#1
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual void __clrcall Foo(String^ value) sealed;
virtual property String^ __clrcall MyProperty
{ String^ get() sealed { String::Empty; } }
};
Interfaces need to be defined as virtual. Also note the "public IMy.." after the class decleration, it's a slighly different syntax than C#.
接口需要定义为虚拟。在课堂变换之后还要注意“公共IMy ..”,它的语法与C#略有不同。
If you can, seal the interface members to improve performance, the compiler will be able to bind these methods more tightly than a typical virtual members.
如果可以,密封接口成员以提高性能,编译器将能够比典型的虚拟成员更紧密地绑定这些方法。
Hope that helps ;)
希望有所帮助;)
I did not compile it but looks good to me... Oh and also, defining your methods as __clrcall eliminates dangers of double thunk performance penalties.
我没有编译它,但对我来说看起来很好......哦,并且,将__clrcall定义为方法可以消除双重性能损失的危险。
edit the correct syntax for a property is:
编辑属性的正确语法是:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed { return String::Empty; };
void set( String^ s ) sealed { };
}
};
or, when putting the definition in the source file:
或者,当将定义放在源文件中时:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed;
void set( String^ s ) sealed;
}
};
String^ MyConcreteClass::MyProperty::get()
{
return String::Empty;
}
void MyConcreteClass::MyProperty::set( String^ )
{
//...
}
#1
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual void __clrcall Foo(String^ value) sealed;
virtual property String^ __clrcall MyProperty
{ String^ get() sealed { String::Empty; } }
};
Interfaces need to be defined as virtual. Also note the "public IMy.." after the class decleration, it's a slighly different syntax than C#.
接口需要定义为虚拟。在课堂变换之后还要注意“公共IMy ..”,它的语法与C#略有不同。
If you can, seal the interface members to improve performance, the compiler will be able to bind these methods more tightly than a typical virtual members.
如果可以,密封接口成员以提高性能,编译器将能够比典型的虚拟成员更紧密地绑定这些方法。
Hope that helps ;)
希望有所帮助;)
I did not compile it but looks good to me... Oh and also, defining your methods as __clrcall eliminates dangers of double thunk performance penalties.
我没有编译它,但对我来说看起来很好......哦,并且,将__clrcall定义为方法可以消除双重性能损失的危险。
edit the correct syntax for a property is:
编辑属性的正确语法是:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed { return String::Empty; };
void set( String^ s ) sealed { };
}
};
or, when putting the definition in the source file:
或者,当将定义放在源文件中时:
public ref class MyConcreteClass : public IMyInterface
{
public:
virtual property String^ MyProperty
{
String^ get() sealed;
void set( String^ s ) sealed;
}
};
String^ MyConcreteClass::MyProperty::get()
{
return String::Empty;
}
void MyConcreteClass::MyProperty::set( String^ )
{
//...
}