C++ 的 const和const_cast

***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************

今天,逛了逛 问答社区,

在C++ 里,看到有人问关于 const_cast 的东西,

正好在 <<Effective C++>> 中,也讲到过这方面的东西。

翻了翻书,上网搜了搜,

发现还挺好玩....

题主问的是如何将一个设定为 const 的 double 类型数组  在运行期间  对它再解除const,然后改动数组内容。

> 首先,明确一下,

对于 const_cast

这个东西,只是对于指针 和 引用 解const,对于变量,就会出现问题。

比如,看下面这段代码:

const int a=789 ;
int &b = const_cast<int&>(a);
int *c = const_cast<int*>(&a);

cout<<"a="<<a<<endl;
cout<<"&b="<<b<<endl;
cout<<"*c="<<*c<<endl;
cout << "&a="<<&a<<endl;
cout << "&b="<<&b<<endl;
cout << "c="<<c<<endl;
cout<<endl;

b = 987;
*c = 999;
cout << "a="<<a<<endl;
cout << "b="<<b<<endl;
cout << "*c="<<*c<<endl;
cout << "&a="<<&a<<endl;
cout << "&b="<<&b<<endl;
cout << "c="<<c<<endl;

运行一下:

很好玩吧~。~

> 然后,对于这个问题,

因为是数组,数组属于指针的范畴了,

我就试着写了写,

发现,

通过一个中间变量,还是可以改动原来的const的内容的:

const double arr[3] = {1.2,3.3,4.5};
int i;
for(i=0;i<3;++i)
	cout<<arr[i]<<" ";
cout<<endl;

double& temp = const_cast<double&>(arr[0]);

for(i=0;i<3;++i)
	cout<<arr[i]<<" ";
cout<<endl;

cout<<temp<<endl;
cout<<arr[0]<<endl;
cout<<&temp<<endl;
cout<<&arr[0]<<endl;

结果还是可以的

OK,就是这样,

挺有意思的东东~。~

***************************************转载请注明出处:http://blog.csdn.net/lttree********************************************

时间: 2024-12-18 16:06:23

C++ 的 const和const_cast的相关文章

C++ this与const,const_cast,static_cast的关系

一.整体代码 #include <iostream> 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 << &qu

invalid conversion from `const void*&#39; to `void*&#39;

在编译一个工程时出错,使用memcpy函数处报错 invalid conversion from `const void*' to `void*' void image2mat(const image<uchar>* I, cv::Mat& img){ int width = I->width(); int height = I->height(); img.create(height, width, CV_8UC1); memcpy(img.datastart, (cha

const_cast, reinterpret_cast, static_cast的用法

/////////////////////////////////////////////////////////////////////////////// // // FileName : cast_item27.cpp // Version : 0.10 // Author : Ryan Han // Date : 2013/10/31 15:43:55 // Comment : // ////////////////////////////////////////////////////

More Effective C++ 条款17 考虑使用lazy evaluation(缓式评估)

1. lazy evaluationg实际上是"拖延战术":延缓运算直到运算结果被需要为止.如果运算结果一直不被需要,运算也就不被执行,从而提高了效率.所谓的运算结果不被执行,有时指只有部分运算结果被需要,那么采用拖延战术,便可避免另一部分不被需要的运算,从而提高效率,以下是lazy evaluation的四种用途. 2. Reference Counting(引用计数) 如果要自己实现一个string类,那么对于以下代码: String s1="Hello"; S

《Effective C++ 》学习笔记——条款03

***************************************转载请注明出处:http://blog.csdn.net/lttree******************************************** 一. Accustoming Yourself to C++ Rule 03: Use const whenever possible. 条款03:尽可能的使用const const就是常量,它允许你指定一个语义约束,编译器会强制实施这项约束. 多才多艺的关键字

第十六课、顺序存储结构的抽象实现----------狄泰软件学院

一.课程目标 1.完成顺序存储结构的抽象实现,既然是抽象实现,自然就是抽象类,不能生成对象 (1).抽象类模板,存储空间的位置和大小由子类完成  (2).这里只实现顺序存储结构的关键操作(增.删.查等) (3).提供数组操作符,方便快速获取元素(要提供const版本的,方便const对象调用) 二.具体实现 这里需注意几点 (1).这里将capacity()函数设置为纯虚函数,说明在这里还不需要实现它,将其留到子类中在实现. (2).数组操作符的返回值一个是引用一个是值,是因为const对象不能

【C++】双向线性链表容器的实现

// 双向线性链表容器 #include <cstring> #include <iostream> #include <stdexcept> using namespace std; // 链表类模板 template<typename T> class List { public: // 构造.析构.支持深拷贝的拷贝构造和拷贝赋值 List(void) : m_head(NULL), m_tail(NULL) {} ~List(void) { clear

iOS装13-之多线程

全栈 线程与进程 开始之前先问自己几个问题 1.大学用C语言写的main函数里只写了hello word ,面有线程么 2.CPU频率和个数与多线程有什么关系 3.进程和线程的关系 4.线程的访问权限 ,它都可以访问哪些东西 5.线程同步 都有哪几种锁 6.线程中都有哪些坑需要注意 大学用C语言写的main函数里只写了hello word ,面有线程么 In a non-concurrent application, there is only one thread of execution.

【C++】智能指针的实现

一点都不智能的智能指针 #include <iostream> using namespace std; template<typename T> class Auto { public: // 避免编译器报错,需要显式类型转换 explicit Auto(T* data = NULL) : m_data(data) {} // 转移拷贝 Auto(Auto<T>& that) : m_data(change(that)) {} // 转移赋值 Auto&