C++ cli 如何在c++/cli
我想将该行从c#转换为c++/cliC++ cli 如何在c++/cli,c++-cli,C++ Cli,我想将该行从c#转换为c++/cli Idocobj is IPart IPart是一个接口,Idocobj是一个对象。是否有任何方法进行此转换 我使用了以下代码: Idocobj->GetType() == IPart::typeid 但它不起作用您可以使用动态\u cast检查“是”。以下是一个例子: using namespace System; namespace NS { public interface class IFoo { void Test();
Idocobj is IPart
IPart是一个接口,Idocobj是一个对象。是否有任何方法进行此转换
我使用了以下代码:
Idocobj->GetType() == IPart::typeid
但它不起作用您可以使用
动态\u cast
检查“是”。以下是一个例子:
using namespace System;
namespace NS
{
public interface class IFoo
{
void Test();
};
public ref class Foo : public IFoo
{
public: virtual void Test() {}
};
public ref class Bar
{
public: virtual void Test() {}
};
}
template<class T, class U>
bool isinst(U u) {
return dynamic_cast< T >(u) != nullptr;
}
int main()
{
NS::Foo^ f = gcnew NS::Foo();
NS::Bar^ b = gcnew NS::Bar();
if (isinst<NS::IFoo^>(f))
Console::WriteLine("f is IFoo");
if (isinst<NS::IFoo^>(b) == false)
Console::WriteLine("f is not IFoo");
Console::ReadKey();
}
Thks回复时,我使用了动态模式来解决问题
NS::IFoo^ ifoo = dynamic_cast<NS::IFoo^>(f);
if (ifoo != nullptr)
{
// Do something...
ifoo->Test();
}