通用代码中与C#默认值等效的C ++ [英] C++ equivalent of C# default in generic code

查看:51
本文介绍了通用代码中与C#默认值等效的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将C#代码转换为CLI/C ++.我正面临一个问题.有一些泛型使用了default关键字.

I have converted C# code to CLI/C++. I am facing one issue. There are some generics where default keyword is used.

public class GenericList
{
    public T GetNext()
    {
        T temp = default(T);
}


在这里,默认关键字对于引用类型将返回null,对于数值类型将返回零.

当我将代码转换为C ++时,无法找到可以用来替代它的等效构造.在这里可以做什么?

[edit]添加了代码块以保留格式-OriginalGriff [/edit]


Here default keyword will return null for reference types and zero for numeric value types.

When I convert the code to C++, I am not able to find equivalent construct which can be used to replace it. What can be done here?

[edit]Code block added to preserve formatting - OriginalGriff[/edit]

推荐答案

由于C ++中没有等效的代码,因此必须使用其他代码.这可以帮助:
http://stackoverflow.com/questions/3693363/cs-default-keyword-equivalent- in-c [ ^ ]
Since there is no equivalent in C++, you must use something different. This can help:
http://stackoverflow.com/questions/3693363/cs-default-keyword-equivalent-in-c[^]


我相信您是在问等效的C ++/CLI语法,如果可以的话,请问:

I believe you were asking about the equivalent C++/CLI syntax, if so here goes:

generic<typename T> ref class GenericList
{
public:
    T GetNext()
    {
        T temp = T();
        return temp;
    }
};



在上面的代码片段中,T()与C#中的default(T)相同,并将生成完全相同的msil.



In the above code snippet T() is identical to default(T) in C#, and will generate the exact same msil.


这篇关于通用代码中与C#默认值等效的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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