什么是相当于C#的默认(T)的C ++ / CLI? [英] What is the C++/CLI equivalent to C#'s default(T)?
本文介绍了什么是相当于C#的默认(T)的C ++ / CLI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正与一些C ++ / CLI code(新语法),我试图声明一个泛型类型,并要设置一个成员变量到它的默认值。
在C#:</ P>
Class类&LT; T&GT;
{
t。成员=默认(T);
}
什么是等值的CLI?
通用&LT; typename的T&GT;公共引用类级
{
上市:
类别():成员(默认值(T))//&LT; - 无worky
{
}
私人:
t。成员;
};
解决方案
有趣的是语法使得它看起来像这样: T()
。它确实需要增加一个拷贝构造函数。
通用&LT; typename的T&GT;
公共引用类级
{
上市:
类别():成员(T())
{
}
类(类^ C)
{
成员= C-&GT;成员;
}
私人:
t。成员;
};
修改 DOH 的这个工程太(已在C#中的土地这么久我忘了NULL和0是一样的东西在C ++中,因此用户无需为不同的值和引用类型的默认值):
通用&LT; typename的T&GT;
公共引用类级
{
上市:
类():部件(0)
{
}
类(类^ C)
{
成员= C-&GT;成员;
}
私人:
t。成员;
};
I'm working with some C++/CLI code (new syntax) and am trying to declare a generic type and want to set a member variable to it's default.
In C#:
class Class<T>
{
T member = default(T);
}
What's the equivalent in CLI?
generic<typename T> public ref class Class
{
public:
Class() : member(default(T)) // <-- no worky
{
}
private:
T member;
};
解决方案
Interestingly enough the syntax makes it looks like this: T()
. It does require the addition of a copy constructor.
generic<typename T>
public ref class Class
{
public:
Class() : member(T())
{
}
Class(Class^ c)
{
member = c->member;
}
private:
T member;
};
Edit DOH This works too (been in C# land for so long I forgot that NULL and 0 are the same thing in C++, hence no need for different value and reference type default values):
generic<typename T>
public ref class Class
{
public:
Class() : member(0)
{
}
Class(Class^ c)
{
member = c->member;
}
private:
T member;
};
这篇关于什么是相当于C#的默认(T)的C ++ / CLI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文