`
dogasshole
  • 浏览: 844033 次
文章分类
社区版块
存档分类
最新评论

const与efficiency&design

 
阅读更多

const意味着不能修改;

我们的潜意识里面要深深刻着:const是
1,design的意图
2,防御式编程,更安全

而const_cast则是对于初始设计的一种破坏,是程序完美性的污染者,hack!

除此之外,可以考虑使用const带来的optimization:
1,compile time:在effect c++中所介绍的,用const来代替#define,也就是预编译时间
而且直接声明一个POD变量为const也有可能促使compiler预处理的时候把变量优化掉;
2,runtime memory:
read-only memory读取的时候据说会比ram快一些,在wikipedia等地方并没有见到确切的说法;
如果真的如此的话,那么compiler在声明POD变量的情况下可能会把变量放到read-only memory中,
从而使得程序运行可能加快;

其他情况下,cosnt对于compiler来说会阻止coder来改变const所修饰的值,但在const的管辖范围
之外和const_cast的存在的情况下,变量与内存都可能被修改,所以const对于编译器来说除非非常的显而易见的情况,否则并不意味着真的const,compiler自己也无从下手去做什么,但是至少这是对于compiler的一个提示,一个更加准确的修饰,更加aggressive的compiler可以在这些方面有所作为,

而且,const带来更加安全,使得程序开发效率提高,那么我们在单位时间内有更多的时间去profile和optimize,这也是一种优化,isn't?

所以const对于coder来说意味着更加明确的设计意图和防御,和潜在的略微优化;

分享到:
评论

相关推荐

    asp网站集锦你肯定喜欢

    Const adOpenForwardOnly = 0 Const adOpenKeyset = 1 Const adOpenDynamic = 2 Const adOpenStatic = 3 '---- CursorOptionEnum Values ---- Const adHoldRecords = &H00000100 Const adMovePrevious = &H00000200...

    Excel VBA常数变量集合

    Public Const xlAll = &HFFFFEFF8 Public Const xlAutomatic = &HFFFFEFF7 Public Const xlBoth = 1 Public Const xlCenter = &HFFFFEFF4 Public Const xlChecker = 9 Public Const xlCircle = 8 Public ...

    C++自己实现的字符串类

    friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); ...

    C++中的模拟class string类的代码 cpp

    friend const string & operator+(const string &s1,const string &s2); friend std::ostream & operator(std::ostream &theStream,const string & str); friend const string & operator+(const string &s1,...

    自定义的矩阵类,内含源码与测试工程

    具体用法与测试用例Main.cpp里有3个测试用例,分别是针对构造函数属性计算与运算符重载的 内已包含测试工程xp\vc6.0\上亲测通过,并经过BoundsChecker测试没有内存泄漏。有兴趣的童鞋可以下作参考。 注意: 1、...

    火车票售票系统

    const>>+ inline Id :int <<const>>+ inline Name :string <<const>>+inline Salary :float <<const>>+inline Allowance :float <<const>>+inline Bonus...

    FTP下载,基于socket实现.

    int Get( const string & strRemoteFile , const string & strLocalFile , const unsigned int nThreadNumber ) ; //函数说明:上载Ftp文件,支持断点方式上传。 int Put( const string & strLocalFile , const ...

    一维动态数组实现的矩阵类

    具体用法与测试用例Main.cpp里有3个测试用例,分别是针对构造函数属性计算与运算符重载的 内已包含测试工程xp\vc6.0\上亲测通过,并经过BoundsChecker测试没有内存泄漏。有兴趣的童鞋可以下作参考。 注意: 1、...

    基于web房产信息管理系统

    Const adHoldRecords = &H00000100 Const adMovePrevious = &H00000200 Const adAddNew = &H01000400 Const adDelete = &H01000800 Const adUpdate = &H01008000 Const adBookmark = &H00002000 Const ...

    网上图书销售数据库+ASP

    '-------------------------------------------------------------------- ' Microsoft ADO ' ...' ' ' ' ADO constants include file for VBScript ' '------------------------------------------------------...Const ...

    C++自编String类代码

    String(const char*const); String(int x); String(const String &rs); int getlen()const{return len;} const char* getstr()const{return str;} char& operator[](int lenth); char operator[](int lenth)...

    深入const int *p与int * const p的区别详解(常量指针与指向常量的指针)

    对于指针和常量,有以下三种形式都是正确的: 代码如下:const char * myPtr = &char_A;//指向常量的指针char * const myPtr = &char_A;//常量的指针const char * const myPtr = &char_A;//指向常量的常量指针下面...

    复数运算符.vcxproj

    friend Complex operator + (const double& d, const Complex& c); friend Complex operator - (const double& d, const Complex& c); friend Complex operator + (const Complex& c, const double& d); friend ...

    c#中const与readonly区别

    c#中const与readonly区别

    Numerical Recipes in cpp for Financial

    date(const int& d, const int& m, const int& y); bool valid(void) const; int day() const; int month() const; int year() const; void set_day (const int& day ); void set_month (const int& month...

    C++ 中const对象与const成员函数的实例详解

    C++ 中const对象与const成员函数的实例详解 const对象只能调用const成员函数: #include using namespace std; class A { public: void fun()const { cout<<const 成员函数!<<endl; } void fun()...

    const与指针的关系

    该文档讲述了const与指针的关系,const起了如何的作用。

    Const,Const函数,Const变量,函数后面的Const.txt

    Const,Const函数,Const变量,函数后面的Const Const,Const函数,Const变量,函数后面的Const

    vc常用函数-类的构造函数

    string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而...

    (const)方法

    通过代码段实例讲解const的用法,应注意的情况

Global site tag (gtag.js) - Google Analytics