I'm trying to overload the assignment operator to work with my class "MyFloat" which is just a string of numbers that represents a value between 0 and 1. when a program tries to use this though, it crashes and i cant seem to figure out why.
I would really appreciate some help with this one.
1 2 3 4 5 6 7 8 9 10 11 12 13
const MyFloat &MyFloat::operator=(constchar Number[]) const
{
int i=1;
for (i; Number[i]!='.'; ++i);
++i;
for (int k=1; MyFloat(*this).Number[k]; ++k)
{
MyFloat(*this).Number[k]=(Number[i]-48);
++i;
}
return MyFloat(*this);
}
const MyFloat &MyFloat::operator=(constchar Number[]) const
explain the purpose of the last const (the bold one).
MyFloat(*this).Number[k]
¿what do you think you are doing there?
int i=1;
array index go from 0 to n-1, so if you've got int answer[42], then trying to access answer[0] is valid, but answer[42] would produce undefined behaviour