Reputation: 23
In C# one could write something like this:
public static class Test
{
}
public class Test<T>
{
}
How can I do the same in C++/CLI?
I tried to define:
public ref class Test abstract sealed
{
};
generic<typename T>
public ref class Test
{
};
But I get a C2011 error: "'class' type redefinition.
I want to create a static class which defines convenient methods for creating instance of a generic class, just like System.Tuple.
Any help would be greatly appreciated!
Upvotes: 2
Views: 711
Reputation: 942020
To be clear, you want to do this:
generic<typename T>
public ref class Test {};
generic<typename T, typename U>
public ref class Test {};
You can't do that in C++/CLI. It is a flaw, given that the CLR allows this. But it is one it inherited from C++. The C++/CLI language uses a lot of plumbing of the C++ compiler, inheriting its naming rules in the process. You can post to connect.microsoft.com but that will be closed quickly as "by design".
The only decent way forward is to move these generic types into a C# class library that you reference in your C++/CLI project. Or by settling for a compromise like renaming the rest of the classes, something like Test2 is palatable. Which does reflect the reality that these classes are entirely unrelated.
Upvotes: 3
Reputation: 8207
There is no concept of static class in C++ , not even in C++/CLI.This has already been discussed here.
However if you want to simulate things as in static class,a workaround is to put some static function, which can be called to initialize static members without creating instances.
class Test
{
public:
static int InitializeGenerics(...);
}
This can be called easily as Test::InitializeGenerics()
Upvotes: 0