首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

【100求教】const_cast的有关问题

2012-03-08 
【100求教】const_cast的问题!书上说“const_cast用法:const_cast type_id(expression)该运算符用来修改类

【100求教】const_cast的问题!
书上说“    
const_cast    
用法:const_cast <type_id>     (expression)    
该运算符用来修改类型的const或volatile属性。除了const     或volatile修饰之外,     type_id和expression的类型是一样的。    
常量指针被转化成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然指向原来的对象;常量对象被转换成非常

量对象。”    
 
但是我编了一个程序测试的时候出了点错误,望高手指点!    
希望高手们不要鄙视我,按书上来总是出错。能把const_cast的问题讲明白的100分送上!
////////////////////////////////////////////////////////    
///Classes.h头文件    
#ifndef     CLASSES_H_        
#define     CLASSES_H_    
 
#include     <iostream>    
using     std::cout;    
using     std::cin;    
using     std::endl;    
 
class     CTest    
{    
public:    
                      int     m_IntNum;    
                      CTest(){    
                                              cout < < "++++++Constructor     here++++++\n ";    
                                              m_IntNum=5;    
                      }    
                      ~CTest(){    
                                              cout < < "------Destructor     here-------\n ";    
                      }    
};    
#endif    
///////////////////////////////////////////    
///main.cpp    
#include     <iostream>    
#include     "Classes.h "    
using     std::cout;    
using     std::endl;    
using     std::cin;    
 
int     main()    
{    
                         
              const     int     a     =     100;    
              int     b     =     const_cast <int> (a);//Error:cannot     convert     from     'const     int '     to     'int '    
                 
              const     CTest     T1;    
              CTest     T2     =     const_cast <CTest> (T1);//cannot     convert     from     'const     class     CTest '     to   'class     CTest '    


/*
我想知道为什么转换不成功?非得用指针吗??    
“常量对象被转换成非常量对象。”这句话有错吗?    
*/    
                         
return     0;    
}

[解决办法]
const_cast Operator
const_cast < type-id > ( expression )
The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.

A pointer to any object type or a pointer to a data member can be explicitly converted to a type that is identical except for the const, volatile, and __unaligned qualifiers. For pointers and references, the result will refer to the original object. For pointers to data members, the result will refer to the same member as the original (uncast) pointer to data member. Depending on the type of the referenced object, a write operation through the resulting pointer, reference, or pointer to data member might produce undefined behavior.

You cannot use the const_cast operator to directly override a constant variable 's constant status.

The const_cast operator converts a null pointer value to the null pointer value of the destination type.

Example
#include <iostream.h>

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;
}

void main() {
CCTest X;
X.setNumber( 8 );
X.printNumber();
}

On the line containing the const_cast, the data type of the this pointer is const CCTest *. The const_cast operator changes the data type of the this pointer to CCTest *, allowing the member number to be modified. The cast lasts only for the remainder of the line on which it appears.
[解决办法]
你可以这样考虑一下,如果你是设计者既然知道要修改这个值就不应该把它定义成const 类型,
上面的根本不需要转换就可以完成复制,为什么要多出来一个显示转换呢?
显示转换的其实是一种间接转换,就比如 :
const int a=20;
int *b;
b= const_cast <int *> (&a);
*b =10;
其实,a的值并没有真正的改变,a是被放在一个const数据段的,是不能被改变的,编译器只是给b临时分配了一个变量给它。其实转换的目的是可以传值,并不是真正改变const对象的值!

说的不对的地方,请大家批评。。。

热点排行