j****g 发帖数: 597 | 1 An interview question.
First he asked what is a const function. I said it's a function that doesn't
change a variable's value.
Then he asked what if you want to change that value in the const function? I
said use mutable. He told me there's other way.
Any idea what is the other way? ? |
k****f 发帖数: 3794 | 2 mutable修饰一下那个变量
't
I
【在 j****g 的大作中提到】 : An interview question. : First he asked what is a const function. I said it's a function that doesn't : change a variable's value. : Then he asked what if you want to change that value in the const function? I : said use mutable. He told me there's other way. : Any idea what is the other way? ?
|
t****t 发帖数: 6806 | 3 如果你叙述没问题, 那就是他问得不对
本身一个function没有const 不const的
类成员方法可以用const修饰, 那也不是"doesn't change a variable's value", 而是说
this的类型是const T * const, 就是不能改变类成员
't
I
【在 j****g 的大作中提到】 : An interview question. : First he asked what is a const function. I said it's a function that doesn't : change a variable's value. : Then he asked what if you want to change that value in the const function? I : said use mutable. He told me there's other way. : Any idea what is the other way? ?
|
b********n 发帖数: 609 | 4 这位不看贴啊。
【在 k****f 的大作中提到】 : mutable修饰一下那个变量 : : 't : I
|
j****g 发帖数: 597 | 5 en, 我觉得他的implication was what thrust said.
Then the question is is there any way to change the value of the member
variable? |
b********n 发帖数: 609 | 6 估计只能const_cast了。
【在 j****g 的大作中提到】 : en, 我觉得他的implication was what thrust said. : Then the question is is there any way to change the value of the member : variable?
|
a******e 发帖数: 95 | 7 For fun only. Not portable.
void Test() const
{
int *b = (int*)(int)(&m_num);
*b++;
}
【在 b********n 的大作中提到】 : 估计只能const_cast了。
|
b********n 发帖数: 609 | 8 和const_cast有啥区别?
【在 a******e 的大作中提到】 : For fun only. Not portable. : void Test() const : { : int *b = (int*)(int)(&m_num); : *b++; : }
|