boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ Q83: 这个const_cast什么意思?
相关主题
问一道无聊的bloomberg电面题
C++ Q42: (C22)
C++: Q75 copy constructor 问什么用 const reference?
const_reverse_iterator和reverse_iterator有什么区别?
one C++ question
请教C/C++小
请问关于overloading << (转载)
发个电话面经
[合集] 几个面试中碰到的问题
Why I can't compile this function successfully
相关话题的讨论汇总
话题: const话题: std话题: int话题: cout话题: cast
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
这个const_cast什么意思?是说a不是常数了吗?
const_cast(a) = other.a;
*****************
class A{
const int a;
public:
A():a(0){};
A(int m_a):a(m_a){};
A& operator =(const A &other);
};
A & A::operator =(const A &other)
{
const_cast(a) = other.a;
return *this;
}
m********l
发帖数: 4394
2
turn const-ness on AND off.
C++'s gay hack.

【在 c**********e 的大作中提到】
: 这个const_cast什么意思?是说a不是常数了吗?
: const_cast(a) = other.a;
: *****************
: class A{
: const int a;
: public:
: A():a(0){};
: A(int m_a):a(m_a){};
: A& operator =(const A &other);
: };

l*****a
发帖数: 14598
3
cast away the constness
otherwise,you can't change the value of a

【在 c**********e 的大作中提到】
: 这个const_cast什么意思?是说a不是常数了吗?
: const_cast(a) = other.a;
: *****************
: class A{
: const int a;
: public:
: A():a(0){};
: A(int m_a):a(m_a){};
: A& operator =(const A &other);
: };

c**********e
发帖数: 2007
4
这个说法不错。是不是先去掉constness,赋值后再弄回来。最终还是const,是不是?

【在 m********l 的大作中提到】
: turn const-ness on AND off.
: C++'s gay hack.

c**********e
发帖数: 2007
5
是这样,但最后a还是常数吗?

【在 l*****a 的大作中提到】
: cast away the constness
: otherwise,you can't change the value of a

f*****i
发帖数: 835
6
tried
const int a = 2;
const_cast(a) = 3;
if print out:
std::cout << a;
shows 2;
std::cout << const_cast(a);
shows 3;
why??
Thanks.
S**I
发帖数: 15689
7
The result is undefined behavior. When a variable is declared const, the
compiler can store the variable anywhere it wants (may be read-only memory
location), when you cast away the constness of this variable and make
assignment, the compiler is forced to create a variable on the stack. So in
your example, a and const_cast(a) could be two different variables and
have different memory location.
If you run the following code, you can see the difference:
const int a = 2;
int b = &const_cast(a);
b = 3;
cout << a << endl;
cout << b << endl;

【在 f*****i 的大作中提到】
: tried
: const int a = 2;
: const_cast(a) = 3;
: if print out:
: std::cout << a;
: shows 2;
: std::cout << const_cast(a);
: shows 3;
: why??
: Thanks.

f*****i
发帖数: 835
8
Thanks, tested following code
const int a = 2;
std::cout << &a << std::endl;

int* b = &(const_cast(a));
*b = 3;
std::cout << &a << std::endl;
std::cout << b << std::endl;
std::cout << a << std::endl;
std::cout << *b << std::endl;
result is:
004EF808
004EF808
004EF808
2
3
super confused now.

in
and

【在 S**I 的大作中提到】
: The result is undefined behavior. When a variable is declared const, the
: compiler can store the variable anywhere it wants (may be read-only memory
: location), when you cast away the constness of this variable and make
: assignment, the compiler is forced to create a variable on the stack. So in
: your example, a and const_cast(a) could be two different variables and
: have different memory location.
: If you run the following code, you can see the difference:
: const int a = 2;
: int b = &const_cast(a);
: b = 3;

S**I
发帖数: 15689
9
I bet if you run a debugger for this example, you will see that both a and *
b have value 3 after "*b = 3", but the output of "std::cout << a" is still 2.
The reason is when compiler sees "std::cout << a", since a is declared as
const and initialized to 2, it may replace this statement with "std::cout <<
2". The compiler never bothers to check whether the value of a has been
modified before "std::cout << a". This is a valid optimization and many
compilers do it.
Change you code to the following and you can see this optimization:
const int a = 2;
std::cout << &a << std::endl;
int* b = &(const_cast(a));
*b = 3;
int c = a;
std::cout << &a << std::endl;
std::cout << b << std::endl;
std::cout << a << std::endl;
std::cout << *b << std::endl;
std::cout << c << std::endl;
Try to modify a reference to a constant by casting away its constness is
undefined behavior, which means anything can happen. In this example it
could be that a is stored on the stack, hence b and &a are the same; but
there is no guarantee that this behavior will remain the same in a different
program or when a different compiler is used.

memory

【在 f*****i 的大作中提到】
: Thanks, tested following code
: const int a = 2;
: std::cout << &a << std::endl;
:
: int* b = &(const_cast(a));
: *b = 3;
: std::cout << &a << std::endl;
: std::cout << b << std::endl;
: std::cout << a << std::endl;
: std::cout << *b << std::endl;

f*****i
发帖数: 835
10
Thanks,
tried it, c is still 2, but I think I get the idea, it may still because of
optimization.
Basically in my understanding now, it's not that safe to use const_cast. or
there maybe a condition that is safe?

*
2.
<<

【在 S**I 的大作中提到】
: I bet if you run a debugger for this example, you will see that both a and *
: b have value 3 after "*b = 3", but the output of "std::cout << a" is still 2.
: The reason is when compiler sees "std::cout << a", since a is declared as
: const and initialized to 2, it may replace this statement with "std::cout <<
: 2". The compiler never bothers to check whether the value of a has been
: modified before "std::cout << a". This is a valid optimization and many
: compilers do it.
: Change you code to the following and you can see this optimization:
: const int a = 2;
: std::cout << &a << std::endl;

S**I
发帖数: 15689
11
As long as you don't try to modify a declared constant, it is fine. For
example:
void foo(int* p){
//do something with p, but do not change what p points to
}
int main (){
const int a = 2;
const int* b = &a;
foo(b); //error! foo expects int*, not const int*
int* c = const_cast(b);
foo(c); //this is OK
}
Many legacy C codes contain functions like foo, so it is necessary to use
const_cast in these cases.
Another example:
int main(){
int a = 2;
const int* b = &a;
int* c = const_cast(b);
*c = 3;
std::cout << a << endl;
}
In this case, although b points to const int type, but a is not declared as
const, so it is valid to modify a by modifying *c.

of
or

【在 f*****i 的大作中提到】
: Thanks,
: tried it, c is still 2, but I think I get the idea, it may still because of
: optimization.
: Basically in my understanding now, it's not that safe to use const_cast. or
: there maybe a condition that is safe?
:
: *
: 2.
: <<

f*****i
发帖数: 835
12
Thanks.

【在 S**I 的大作中提到】
: As long as you don't try to modify a declared constant, it is fine. For
: example:
: void foo(int* p){
: //do something with p, but do not change what p points to
: }
: int main (){
: const int a = 2;
: const int* b = &a;
: foo(b); //error! foo expects int*, not const int*
: int* c = const_cast(b);

1 (共1页)
进入JobHunting版参与讨论
相关主题
Why I can't compile this function successfully
请教一个c的概念题
问个简单coding问题
C++ 题目
分享A公司面经
问一个C的简单问题
问一个C++问题:default parameter and overriding/inheritanc
C++ Q36: derivation specification (B8_9)
C++ online Test 又一题
C++ template
相关话题的讨论汇总
话题: const话题: std话题: int话题: cout话题: cast