由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 为什么cpp会有 const_cast ?
相关主题
C++ cast 小结python 正则表达式请教
请问这是什么错误呀有人做topcoder open么 ?
c++ 语法问个Excel VBA 的问题
c++ 弱问题:static const char* const 这两个const 分别是什么意思?xcode for leopard BUG??? help.
c warninggcj qualify round今天晚上7:00est截止
问个c++语言扩展的问题 (转载)Amex Blue Cash preferred 6% Cashback+$150bonus超市专用信用卡
please help C++ beginnerAmex Blue Cash preferred 6% Cashback+$150bonus超市专用信用卡
C++ storage classes and qualifiersAmex Blue Cash preferred 6%现金回扣+$150bonus超市专用信用卡
相关话题的讨论汇总
话题: const话题: cctest话题: cast话题: cpp话题: number
进入Programming版参与讨论
1 (共1页)
v****s
发帖数: 1112
1
觉得cpp里面定义这个const_cast有点违规嫌疑,既然const了,干吗还要允许可以修改
?高人给点
拨点拨
// expre_const_cast_Operator.cpp
// compile with: /EHsc
#include
using namespace std;
class CCTest {
public:
void setNumber( int );
void printNumber() const;
private:
int number;
};
void CCTest::setNumber( int num ) { number = num; }
void CCTest::printNumber() const {
cout << "\nBefore: " << number;
const_cast< CCTest * >( this )->number--;
cout << "\nAfter: " << number;
}
int main() {
CCTest X;
X.setNumber( 8 );
X.printNumber();
}
e****d
发帖数: 895
2
"this" pointer has type "const CCTest *" in the print member function,
and it couldn't be used to descrease its number data member directly.

【在 v****s 的大作中提到】
: 觉得cpp里面定义这个const_cast有点违规嫌疑,既然const了,干吗还要允许可以修改
: ?高人给点
: 拨点拨
: // expre_const_cast_Operator.cpp
: // compile with: /EHsc
: #include
: using namespace std;
: class CCTest {
: public:
: void setNumber( int );

p***o
发帖数: 1252
3
Sometimes people need it, though I don't know when, probably for template
meta programming when you need to remove cv-qualifiers.
The example you mentioned is bad anyway since you can use mutable. There's
a very similar example in The C++ PL and BS said so.

【在 v****s 的大作中提到】
: 觉得cpp里面定义这个const_cast有点违规嫌疑,既然const了,干吗还要允许可以修改
: ?高人给点
: 拨点拨
: // expre_const_cast_Operator.cpp
: // compile with: /EHsc
: #include
: using namespace std;
: class CCTest {
: public:
: void setNumber( int );

b******n
发帖数: 592
4
yes, for smart pointers for example, when assignment actually changes the
right-hand object.

【在 p***o 的大作中提到】
: Sometimes people need it, though I don't know when, probably for template
: meta programming when you need to remove cv-qualifiers.
: The example you mentioned is bad anyway since you can use mutable. There's
: a very similar example in The C++ PL and BS said so.

k******r
发帖数: 2300
5
C++ 里面的所谓constness 的目的是把 run-time error 尽可能转变成 compile-time
error,减少无意出错。而你用const_cast 你当然是知道自己在做什么,所以这一点不
矛盾。

【在 v****s 的大作中提到】
: 觉得cpp里面定义这个const_cast有点违规嫌疑,既然const了,干吗还要允许可以修改
: ?高人给点
: 拨点拨
: // expre_const_cast_Operator.cpp
: // compile with: /EHsc
: #include
: using namespace std;
: class CCTest {
: public:
: void setNumber( int );

v****s
发帖数: 1112
6
i don't quite agree your point. what if a lousy coder is changing some const
variables that an experienced coder has designed? how you are going to debug
if you are a manager and dealing with 1,000 cpp files project?

time

【在 k******r 的大作中提到】
: C++ 里面的所谓constness 的目的是把 run-time error 尽可能转变成 compile-time
: error,减少无意出错。而你用const_cast 你当然是知道自己在做什么,所以这一点不
: 矛盾。

k******r
发帖数: 2300
7
A lousy coder can do anything bad, period. C++ is not designed to safeguard
lousy coders. C++ is like any tool which is beautiful only if you use it
properly.
If I am a manager, I get my paychecks and do nothing.

const
debug

【在 v****s 的大作中提到】
: i don't quite agree your point. what if a lousy coder is changing some const
: variables that an experienced coder has designed? how you are going to debug
: if you are a manager and dealing with 1,000 cpp files project?
:
: time

v****s
发帖数: 1112
8
you are a nice manager.

safeguard

【在 k******r 的大作中提到】
: A lousy coder can do anything bad, period. C++ is not designed to safeguard
: lousy coders. C++ is like any tool which is beautiful only if you use it
: properly.
: If I am a manager, I get my paychecks and do nothing.
:
: const
: debug

1 (共1页)
进入Programming版参与讨论
相关主题
Amex Blue Cash preferred 6%现金回扣+$150bonus超市专用信用卡c warning
Amex Blue Cash preferred 6% Cashback+$150bonus超市专用信用卡问个c++语言扩展的问题 (转载)
Amex Blue Cash preferred 6%现金回扣+$150bonus超市专用信用卡please help C++ beginner
Amex Blue Cash preferred 6%现金回扣+$150bonus超市专用信用卡C++ storage classes and qualifiers
C++ cast 小结python 正则表达式请教
请问这是什么错误呀有人做topcoder open么 ?
c++ 语法问个Excel VBA 的问题
c++ 弱问题:static const char* const 这两个const 分别是什么意思?xcode for leopard BUG??? help.
相关话题的讨论汇总
话题: const话题: cctest话题: cast话题: cpp话题: number