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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 觉得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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 觉得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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 觉得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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 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
|