C ++托管代码问题 [英] C++ managed code question

查看:327
本文介绍了C ++托管代码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.

我有一个CLR项目和一个c ++托管类.
我需要模仿c#中"is"关键字的行为,但我不知道如何在托管代码中进行操作.

我的代码的一部分:
1.C#代码:

hi guys.

i have a CLR project and a c++ managed class.
i need to mimic the behavior of the "is" keyword from c# but i dont know how to do it in managed code.

part of my code:
1.C# code:

public class emailMsg : Msg
{
    string email;
}


2.C ++托管代码


2.C++ manged code

Msg^ msg = gcnew Msg();
if (msg is emailMsg)
{
    //DO SOMETHING
}



上面的 if句子未编译,因为托管代码无法识别"is"关键字.
我该如何解决?



the above if sentence is not compiling since managed code dont recognize the "is" keyword.
how can i solve it?

推荐答案

dynamic_cast<T^>(expression)



C#的"is"运算符只不过是"as"强制类型转换,然后检查是否为空,这与在C ++中使用dynamic_cast然后检查是否为nullptr完全相同.



The C# ''is'' operator is nothing more than an ''as'' cast followed by a check for null, which is exactly the same thing you get by using dynamic_cast in C++ followed by a check for nullptr.


这篇关于C ++托管代码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆