C++/CLI generics, use T in array<> and other collections -
I am writing a generic class in C ++ / CLI (VS2008) to store and manage different types of records I need to collect and keep them in the DB / disc / etc before I was trapped in something like this:
Ref Class Record {// .. .}} General & lt; Typename T & gt; Where T: record, GCN (public) public square class {public: // .... functions .... protected: array & lt; T ^ & gt; ^ Stored data; };
The error of the course is failed with the C3229 ( indirections on a common type of parameter is not allowed ) if I delete '^' The error is C3149 ( this type can be used without top level ^ ^ '). It is easily done in VBnet (in fact, I am migrating to the current VBnet class!), But in C ++ I think that has reached a dead end. Is it really impossible in C ++ / CLI?
Thanks in advance.
Do you have to:
public ref class record { }; General & lt; Typename T & gt; Where T: record, GCN (public) public square class {public: // .... functions .... protected: array & lt; T & gt; ^ Stored data; };
Then you can install the collection like this:
factory < Record ^ & gt; ^ Records = gcnew factory & lt; Record ^ & gt; ();
MSDN has to say about generic in C ++:
Both value types (either underlying types such as int or double, or users -Defined value types) and reference types can be used as a general type of argument. Syntax depends on the general definition. Syntactically, the unknown type is considered as if it were a reference type. However, the runtime is capable of determining if the value actually used is a value type and replaces the appropriate generated code for direct access to the members. The value types used in the form of common types of arguments are not boxed and therefore the performance associated with boxing does not affect the penalties. The syntax used within the normal body is instead of T ^ and '->'. ' should be there.
So that means, I think any use of GCNE for type parameter will be considered as a simple form of price type properly by the runtime. That any common type of argument is considered as the indicator of managed class i.e. T ^. In fact, you can not instantaneously trigger a generic class using something like this:
factory & lt; Record & gt; ^ Records = gcnew factory & lt; Record & gt; ();
The compiler will throw this error (even if you delete , where T: record, GCN ()
):
< Code> Error C3225: The usual type of argument can not be 'record' for 't', this should be a value type or handle for reference type
Comments
Post a Comment