I’m writing a generics class in C++/CLI (VS2008) to store and manage records of different kinds and I need collections to keep them before flusing them to DB/disk/etc. I was thinking in something like this:
ref class Record
{
// ...
};
generic<typename T>
where T : Record, gcnew()
public ref class Factory
{
public:
// ....functions....
protected:
array<T^> ^ StoredData;
};
Which of course failed with error C3229 (indirections on a generic type parameter are not allowed). If I remove the ‘^’, the error is then C3149 (cannot use this type here without a top-level ‘^’). This is easily done in VB.Net (in fact, I’m migrating an existing VB.Net class!), but in C++ I seem to have reached a dead end. Is this actually impossible in C++/CLI?
Thanks in advance.
What you need to do is this:
You then instantiate the collection like so:
This is what MSDN has to say about generics in C++:
So this means, I think, that any generic type argument is treated as a pointer to managed class i.e. T^. In fact you cannot instantiate a generic class using something like this:
The compiler will throw this error(even if you remove
where T : Record, gcnew()):