I have this problem:
I would like to overload () operator in such way, that if it is on the left side it will do something different than when it is on the right side.
For example:
T k;
k(0,0) = 5; - it would dynamically allocate memory of array of size 1x1 and assign number 5
int j = k(1,2) - it should return an error, because memory for k(1,2) hasn't been allocated yet, so there is no value for k(1,2).
Is there a way how to decide whether I'm assigning a value or getting a value? Right now when I overload the operator, it allocates new memory in both cases. So if you have any idea how to do this, please let me know.Thanks.
lvalue will always be nonconst -- although I'm not sure if its reliable that rvalue will always be const. This probably isn't the best way to go about this, and you might want to just make a set/get style approach instead.
EDIT -- fixed syntax (twice)
EDIT -- yeah I just tried this and it doesn't work. Unless the object is explicitly declared as const, it seems to default to the nonconst version, even when an rvalue.
Hmm..interesting idea. I was also thinking about the get/set approach, but I just didn't like it so much. Funny thing is that when I do it as you wrote, the VS2008 compiler crashes, so I actually can't say whether your idea is working :-). I guess it can't have 2 same overloaded operators that differ only in const (maybe it is also because the operator is of a template class).