I can run the following code without problem. I notice both a.bytes and bytes in main changed to "B". So what does the "const" do in A's get() function? Is it going to be a problem in my usage of changing the bytes I get from A as below?
Note: I don't care if A's value will change. I just want to know if I will encounter unpredictable problems, especially when I free A at its deconstructor no matter whatever crazy action I take with the bytes in main.
class A{
public:
A(){
bytes = (char *)malloc(12);
bytes[0] = 'A';
bytes[1] = 0;
}
~A(){
free(bytes);
}
char * get() const{
return bytes;
}
char * bytes;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
char * bytes = a.get();
bytes[0] = 'B';
return 0;
}