(1) the integer argument (1024) cannot be constant. A modification of the source code as follows would solve this problem:
int max2 = max;
if (max2 == -1) max2 = kMaxInt;
while (nr < max2-1) {
// etc...
One may argue that this is less elegant. However, the user should be able to call it with either a const or a non-const integer. The customer is always right.
(2) ReadString is not const, therefore the compiler assumes that “buf” could be modified. By looking at the code I don’t think that this is the case, and therefore ReadString could be declared const. Am I wrong?
my_method() fails for the reason that it has a “const TBuffer*” as argument while TBuffer::ReadString() is not const. So to make it work you would either have to change the argument to “TBuffer*” or cast the const away with const_cast<>. Passing “const int” by value is no problem, the ReadString() will see a value on the stack of 1024 and this stack item can be changed without problem. Also ReadString() cannot be made const since there is a call to TBuffer::operator>>(char*) (*this >> ch) which cannot be made const since it changes state in the TBuffer.