Given the following code:
class TestA
{
private:
char Temp;
public:
char *Ptr;
TestA(){Ptr = NULL; Temp = 'A'; Ptr = &Temp;}
void Function(){Ptr = &Temp; Temp = 'B';}
void operator=(const TestA &ItemCopy)
{
//ItemCopy.Temp = 'N'; //Not permitted
printf("%c!\n",ItemCopy.Temp);
Ptr = ItemCopy.Ptr; //This is okay
*Ptr = 'M'; //This is okay, but it re-assigns ItemCopy.Temp. What?
printf("%c!\n",ItemCopy.Temp);
}
};
int main()
{
TestA Temp1,Temp2;
Temp1.Function();
Temp2 = Temp1;
}
Produces the following:
B
M
Even though ItemCopy is const. Why am I permitted to indirectly modify it or even take a non-const copy of the pointer?