Redian新闻
>
how to change a variable's value in a const function
avatar
how to change a variable's value in a const function# Programming - 葵花宝典
j*g
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? ?
avatar
k*f
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? ?

avatar
t*t
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? ?

avatar
b*n
4
这位不看贴啊。

【在 k****f 的大作中提到】
: mutable修饰一下那个变量
:
: 't
: I

avatar
j*g
5
en, 我觉得他的implication was what thrust said.
Then the question is is there any way to change the value of the member
variable?
avatar
b*n
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?

avatar
a*e
7
For fun only. Not portable.
void Test() const
{
int *b = (int*)(int)(&m_num);
*b++;
}

【在 b********n 的大作中提到】
: 估计只能const_cast了。
avatar
b*n
8
和const_cast有啥区别?

【在 a******e 的大作中提到】
: For fun only. Not portable.
: void Test() const
: {
: int *b = (int*)(int)(&m_num);
: *b++;
: }

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。