The following code:
ref class A
{
private:
int% referenceToAnInt;
};
produces the following error in Visual Studio:
error C3160: ‘int %’ : a data member of a managed class cannot have this type
I thought tracking references were a managed thing – so why can’t they be a member of a managed class?
Also: How would I store a reference to a value type in C++/CLI correctly?
The CLR doesn’t allow storing tracking references as fields. Also, from the C++/CLI-Spec:
I guess they wanted to avoid the problem where you keep a reference longer than the actual lifetime of the referenced object. An alternative would be to use a wrapper ref class to hold the value, or delegates for reading / writing.