Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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);
}

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]
Posted
Updated 11-Feb-11 21:33pm
v2
Comments
Sergey Alexandrovich Kryukov 12-Feb-11 23:06pm    
Good question, my 5.
--SA

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[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-11 22:29pm    
There is more in-depth explanation here, my 5.
--SA
I believe you were asking about the equivalent C++/CLI syntax, if so here goes:

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


In the above code snippet T() is identical to default(T) in C#, and will generate the exact same msil.
 
Share this answer
 
v2
Comments
Abhinav S 12-Feb-11 10:45am    
Wow. They have generics in C++?
5.
Nish Nishant 12-Feb-11 10:46am    
In C++/CLI, yes they do.
Abhinav S 12-Feb-11 10:46am    
Ok I think I remembered them now (reading about templates). Never used them though.
Nish Nishant 12-Feb-11 10:46am    
Generics is available only in C++/CLI. Templates can be used in standard C++ as well as in C++/CLI.
Abhinav S 12-Feb-11 12:10pm    
Oh I see. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900