由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - how to change a variable's value in a const function
相关主题
问个问题[合集] 为什么不能: declare a static memeber func
const_cast问题[合集] c++ 面试题一问
question about const reference[合集] What are the differences between "const char* p" and "c
[合集] static const代替define的performance tradeoff在哪里?function declaration
c++ iterator 弱问python 区别于其他语言的根本?
问个c++问题[合集] one simple question
(面试题) 给code挑毛病(updated with multiple choices)[合集] Effective C++上说要少用casting,但是polymorphism一定要用到c
const in c++关于const和volatile修饰变量或指针的问题
相关话题的讨论汇总
话题: const话题: function话题: change话题: variable话题: value
进入Programming版参与讨论
1 (共1页)
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++;
: }

1 (共1页)
进入Programming版参与讨论
相关主题
关于const和volatile修饰变量或指针的问题c++ iterator 弱问
clarify问个c++问题
有关objec access path的问题(面试题) 给code挑毛病(updated with multiple choices)
why int** cannot convert to const int** ?const in c++
问个问题[合集] 为什么不能: declare a static memeber func
const_cast问题[合集] c++ 面试题一问
question about const reference[合集] What are the differences between "const char* p" and "c
[合集] static const代替define的performance tradeoff在哪里?function declaration
相关话题的讨论汇总
话题: const话题: function话题: change话题: variable话题: value