I use operator() as a subscript operator this way:
double CVector::operator() (int i) const
{
if (i >= 0 && i < this->size)
return this->data[i];
else
return 0;
}
double& CVector::operator() (int i)
{
return (this->data[i]);
}
It works when I get values, but I get an error when I try to write assign a value using
a(i) = 1;
UPD: Error text:
Unhandled exception at 0x651cf54a
(msvcr100d.dll) in CG.exe: 0xC0000005:
Access violation reading location
0xccccccc0.
Like I said in my comment, the problem is your flawed design. I make a 100% guarantee on one of two things:
datais pointing to invalid space in memory.In either case, I would suggest adding:
and adding
assert(i >= 0 && i < this->size)instead of the silent failures: