开发者

What is the C++/CLI equivalent to C#'s default(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&l开发者_如何学运维t;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;
};


But isn't the private member already initialized with the default constructor?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜