avatar.ds
July 11th, 2006, 03:20 PM
I need to create a public operator[] for a managed class that will provide an assignable access to a private array within that class, i.e. it should return an l-value. This is doable in standard c++ using the reference operator:
class Class
{
private: int Array[1];
public: int& operator[](int Index){return (Array[Index]);};
void Try(){Class C; C[0] = 1;};//note:rC[0] is l-value
}; //OK
With CLI I get a strange error:
ref class rClass
{
private: array <int>^rArray;
public: int& operator[](int rIndex){return rArray[rIndex];}//error here!!!
void rTry(){rClass^rC;rC[0]=1;}//note:rC[0] is STILL an l-value, no error here
};//error 2440: cannot convert from 'int' to 'int &'
Is there a workaround?
class Class
{
private: int Array[1];
public: int& operator[](int Index){return (Array[Index]);};
void Try(){Class C; C[0] = 1;};//note:rC[0] is l-value
}; //OK
With CLI I get a strange error:
ref class rClass
{
private: array <int>^rArray;
public: int& operator[](int rIndex){return rArray[rIndex];}//error here!!!
void rTry(){rClass^rC;rC[0]=1;}//note:rC[0] is STILL an l-value, no error here
};//error 2440: cannot convert from 'int' to 'int &'
Is there a workaround?